gpt4 book ai didi

cmake - 如何在 CMake 的 try_compile 函数中设置 OpenMP 的链接器标志

转载 作者:行者123 更新时间:2023-12-02 17:40:16 25 4
gpt4 key购买 nike

我想验证当前的编译器是否可以使用 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

最佳答案

CMake 有一个 standard module用于测试编译器是否支持 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}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()

注意:
不建议再使用此答案将 OpenMP 包含在当前 CMake 版本的项目中。引用其他答案。

关于cmake - 如何在 CMake 的 try_compile 函数中设置 OpenMP 的链接器标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12399422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com