我有一个非常简单的openmp hello world程序:
#include <stdio.h>
#include <omp.h>
int main()
{
#pragma omp parallel
{
int id = omp_get_thread_num();
printf("hello, from %d.\n", id);
}
return 0;
}
我可以使用gcc-5 -fopenmp hello.c -o hello.o
但我想在CLion中编码,它使用CMake来组织项目,当我点击运行按钮时,我会收到错误
fatal error: 'omp.h' file not found
#include <omp.h>
我在google中搜索过,并在我的CMakeLists.txt文件中添加了一些东西
这是我的CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.3)
project(openmp)
OPTION (USE_OpenMP "Use OpenMP" ON)
IF(USE_OpenMP)
FIND_PACKAGE(OpenMP)
IF(OPENMP_FOUND)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
ENDIF()
ENDIF()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fopenmp")
set(SOURCE_FILES hello.c omp.c)
add_executable(openmp ${SOURCE_FILES})
答案 0 :(得分:1)
您的代码是C而不是C ++,因此不要更改CMAKE_CXX_FLAGS
,而是更改CMAKE_C_FLAGS
:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")
将C编译器设置为gcc-5:
set(CMAKE_C_COMPILER /your/path/to/gcc-5)
要了解gcc-5路径,请在终端中输入which gcc-5