如何在CLion中设置基本的openMP项目

时间:2016-11-10 15:19:48

标签: c++ cmake mingw openmp clion

我正在尝试在OpenMP中运行简单的CLion IDE程序。 当我运行它时,我得到一个错误:

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++的初级程序员,所以请给我一些解释如何设置我的项目。

1 个答案:

答案 0 :(得分:8)

过了一会儿我发现了。我更改了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。