2016-11-10 52 views
5

我試圖在CLion IDE中運行簡單的OpenMP程序。 當我運行它,我得到一個錯誤:如何在CLion中設置基本的openMP工程

CMakeFiles\openmp_test_clion.dir/objects.a(main.cpp.obj): In function `main': 
D:/.../openmp_test_clion/main.cpp:9: undefined reference to 'omp_get_thread_num' 
collect2.exe: error: ld returned 1 exit status 

這裏是我的代碼:

#include <stdio.h> 
#include <omp.h> 

int main() 
{ 
    int id; 
#pragma omp parallel private(id) 
    { 
     id = omp_get_thread_num(); 
     printf("%d: Hello World!\n", id); 
    } 
    return 0; 
} 

這裏是我的CMakeLists.txt

cmake_minimum_required(VERSION 3.6) 
project(openmp_test_clion) 

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 

set(SOURCE_FILES main.cpp) 
add_executable(openmp_test_clion ${SOURCE_FILES}) 

message(STATUS "Checking OpenMP") 
find_package(OpenMP) 
IF(OPENMP_FOUND) 
    message("Found OpenMP!) 
    # add flags for OpenMP 
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") 
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") 
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_SHARED_LINKER_FLAGS}") 
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") 
ELSE() 
    message("Missed OpenMP!") 
ENDIF() 

這裏是我的工具鏈的畫面: enter image description here

我有零經驗與OpenMP和我在C++初學者程序員,所以請給我一些解釋如何設置我的項目。

回答

4

所以過了一段時間我就明白了。我改的CMakeLists.txt如下:

cmake_minimum_required(VERSION 3.6) 
project(openmp_test_clion) 

# added -fopenmp 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fopenmp") 

set(SOURCE_FILES main.cpp) 
add_executable(openmp_test_clion ${SOURCE_FILES}) 

,我需要通過TDM-GCC installer安裝的OpenMP。

+0

[This answer](https://stackoverflow.com/a/12404666/1662425)顯示了一個更好的設置,它支持多個編譯器,而不是硬編碼gcc標誌。 – tera