2017-10-08 103 views
0

我正在学习并行计算,并已开始与OpenMP和C的旅程。Clion和OpenMP

我一直在配置Clion,但没有运气。

#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"); 

}

但我正在此错误:

在功能main': C:/Users/John/CLionProjects/Parallelexamples/main.c:6: undefined reference to omp_get_num_threads' C:/Users/John/CLionProjects/Parallelexamples/main.c:7:未定义参考` omp_get_thread_num' collect2.exe:error:ld returned 1 exit status mingw32-make.exe [2]:* [Parallelexamples.exe] Error 1 CMakeFiles \ Parallelexamples.dir \ build.make:95:recipe for target' Parallelexamples.exe”失败 mingw32-make.exe [1]:* [CMakeFiles/Parallelexamples.dir/all]错误2 CMakeFiles \ Makefile2:66:目标'CMakeFiles/Parallelexamples.dir/all'失败的配方 Makefile:82:配方对于目标 '所有' 失败 的mingw32-的make.exe:*** [全部]错误2

我按照指示,使我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}) 

我错过了什么?

回答

3

首先,因为使用的是CMake的,采取FindOpenMP宏的优势:https://cmake.org/cmake/help/latest/module/FindOpenMP.html

cmake_minimum_required(VERSION 3.8) 
project(Parallelexamples) 

其次,看来你是不是链接到OpenMP运行时库。不仅要你通过openmp编译标记,你还必须通过正确的链接器标志(S):

set_target_properties(Parallelexamples LINK_FLAGS "${OpenMP_CXX_FLAGS}") 

而只是作为一个旁注,如果你真的使用C不是C++编程,你将不再需要您可以使用 C_FLAGS