我想验证当前编译器是否可以使用openmp支持构建。该应用程序已经部署在各种各样的unix系统上,其中一些可能有旧版本的OpenMP,我想测试重要的OpenMP功能。所以,我想构建一个包含一些OpenMP调用的测试源文件。
因此,我创建了一个非常简单的测试文件,并尝试使用CMake的try_compile函数。遗憾的是,它似乎没有正确应用-fopenmp链接器标志。有谁知道如何强制链接器标志或查看链接器标志是否被应用于任何地方?
来自CMakeLists.txt
try_compile(
HAVE_OPENMP
${APBS_ROOT}/src/config
${APBS_ROOT}/src/config/omp_test.c
CMAKE_FLAGS "-DCMAKE_C_FLAGS=-fopenmp -DCMAKE_EXE_LINKER_FLAGS=-fopenmp"
OUTPUT_VARIABLE TRY_COMPILE_OUTPUT
)
来自omp_test.c
#include <stdio.h>
#include <omp.h>
int main()
{
int i;
int threadID = 0;
#pragma omp parallel for private(i, threadID)
for(i = 0; i < 16; i++ )
{
threadID = omp_get_thread_num();
#pragma omp critical
{
printf("Thread %d reporting\n", threadID);
}
}
return 0;
}
结果输出
Change Dir: src/config/CMakeFiles/CMakeTmp
Run Build Command:/usr/bin/make "cmTryCompileExec/fast"
/usr/bin/make -f CMakeFiles/cmTryCompileExec.dir/build.make CMakeFiles/cmTryCompileExec.dir/build
make[1]: Entering directory `src/config/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report /data/work/source/apbs/src/config/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec.dir/omp_test.c.o
/usr/bin/gcc -o CMakeFiles/cmTryCompileExec.dir/omp_test.c.o -c /data/work/source/apbs/src/config/omp_test.c
Linking C executable cmTryCompileExec
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTryCompileExec.dir/link.txt --verbose=1
/usr/bin/gcc CMakeFiles/cmTryCompileExec.dir/omp_test.c.o -o cmTryCompileExec -rdynamic
CMakeFiles/cmTryCompileExec.dir/omp_test.c.o: In function `main':
omp_test.c:(.text+0x19): undefined reference to `omp_get_thread_num'
collect2: ld returned 1 exit status
make[1]: *** [cmTryCompileExec] Error 1
make[1]: Leaving directory `src/config/CMakeFiles/CMakeTmp'
make: *** [cmTryCompileExec/fast] Error 2
CMake Error at CMakeLists.txt:688 (message):
Test OpenMP program would not build. OpenMP disabled
当我尝试在命令行上编译测试程序时,它可以正常工作
src/config$ gcc -fopenmp omp_test.c -o omp_test && ./omp_test
Thread 1 reporting
Thread 4 reporting
Thread 7 reporting
Thread 11 reporting
Thread 9 reporting
Thread 12 reporting
Thread 6 reporting
Thread 8 reporting
Thread 15 reporting
Thread 13 reporting
Thread 10 reporting
Thread 0 reporting
Thread 3 reporting
Thread 2 reporting
Thread 5 reporting
Thread 14 reporting
答案 0 :(得分:115)
如果编译器支持OpenMP,CMake有standard module进行测试:
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}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
注意强>:
建议不要再使用此答案,以便在项目中包含OpenMP
当前CMake
版本。参考例如this问题或其他答案。
答案 1 :(得分:30)
从CMake 3.9开始,每种语言都导入了OpenMP目标。我认为这是一个更为优雅的解决方案。这是C ++中的示例:
cmake_minimum_required(VERSION 3.9)
project(solver LANGUAGES CXX)
find_package(OpenMP REQUIRED)
add_executable(solver solver.cc)
target_link_libraries(solver PRIVATE OpenMP::OpenMP_CXX)
这是更方便的方法,因为它键入的次数更少,而且您不必使用容易出错的编译标志,库等进行调整。这就是现代CMake的发展方向。
如果您使用的版本早于CMake 3.9,我仍然不建议使用currently accepted answer。我相信为每个目标设置标志会更好:
add_executable(solver solver.cc)
target_link_libraries(solver PRIVATE "${OpenMP_CXX_FLAGS}")
target_compile_options(solver PRIVATE "${OpenMP_CXX_FLAGS}")
这可能不适用于某些编译器;这就是部分原因,原因在于CMake在CMake 3.9中改进了对OpenMP的支持。
答案 2 :(得分:5)
如果您尝试在g ++中使用“现代”方式,也可以这样做:
find_package(OpenMP REQUIRED)
add_executable(Foo foo.cpp)
target_compile_options(Foo PRIVATE -Wall ${OpenMP_CXX_FLAGS})
target_link_libraries(Foo PRIVATE ${OpenMP_CXX_FLAGS})
注意:
如果只留下target_compile_options,则编译指示将简单地被忽略(启用的警告会告诉您)
如果您只保留target_link_libraries,则由于g ++链接不正确,您的代码将无法编译
如果您都忽略了1.注释,则将导致不再需要链接,并且代码将被编译。
我不知道这种将“ hack”与标志链接起来是否也适用于其他编译器。
答案 3 :(得分:1)
OpenMP 支持在CMake 3.9+中得到了显着改善
cmake_minimum_required(VERSION 3.9)
project(openmp_test) # you can change the project name
find_package(OpenMP)
add_executable(openmp_para_test main.cpp) # you can change the excutable name
if(OpenMP_CXX_FOUND)
target_link_libraries(openmp_para_test PUBLIC OpenMP::OpenMP_CXX)
endif()
如果需要,这种方式将正确设置库链接行和编译行。