2017-10-08 84 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