我正在学习并行计算,并且已经开始了我的OpenMP和C。
我一直在配置克莱恩,但没有运气。
#include <stdio.h>
#include <omp.h>
int main() {
#pragma omp parallel
{
int n = omp_get_num_threads();
int tid = omp_get_thread_num();
printf("There are %d threads. Hello from thread %d\n", n, tid);
};
/*end of parallel section */
printf("Hello from the master thread\n");}
但是我发现了一个错误:
函数C:/Users/John/CLionProjects/Parallelexamples/main.c:7: omp_get_num_threads‘对`omp_get_thread_num的未定义引用:错误: ld返回了一个退出状态mingw32-make.exe 2:* Parallelexamples.exe错误1,目标'Parallelexamples.exe’失败的配方32-make.exe 1:* CMakeFiles/Parexamplples.dir/all Error 2 CMakeFiles\Makefile2:66:食谱用于目标‘CMakeFiles/Par等位基因示例’. .dir/ all‘failed :82:目标'all’的配方
我遵循了说明,并使我的CMakeListtxt文件如下:
cmake_minimum_required(VERSION 3.8)
project(Parallelexamples)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu11 -fopenmp")
set(SOURCE_FILES main.c)
add_executable(Parallelexamples ${SOURCE_FILES})我错过了什么吗?
发布于 2017-10-08 14:34:49
首先,由于您使用的是CMake,所以要利用FindOpenMP宏:https://cmake.org/cmake/help/latest/module/FindOpenMP.html
cmake_minimum_required(VERSION 3.8)
project(Parallelexamples)其次,您似乎没有链接到OpenMP运行时库。您不仅必须传递openmp编译标志,还必须传递正确的链接器标志:
set_target_properties(Parallelexamples LINK_FLAGS "${OpenMP_CXX_FLAGS}")就像一个侧面,如果你真的在用C而不是C++编程,你就不需要CXX_FLAGS了--你可以只使用C_FLAGS
发布于 2020-01-22 21:50:18
在现代CMake中,您应该使用OpenMP的导入目标。
cmake_minimum_required(VERSION 3.14)
project(Parallelexamples LANGUAGES C)
find_project(OpenMP REQUIRED)
add_executable(Parallelexamples main.c)
target_link_libraries(Parallelexamples PRIVATE OpenMP::OpenMP_C)https://stackoverflow.com/questions/46632072
复制相似问题