include_dirs for OpenMP in CMakeLists

huangapple go评论77阅读模式
英文:

include_dirs for OpenMP in CMakeLists

问题

我遇到了一个启用OpenMP的项目,并且在CMakeLists.txt文件中有以下代码行:

include_directories(${CMAKE_CUR_DIR} /usr/lib/llvm-11/include/openmp/)

我尝试将其更加通用化,如下所示:

find_package(OpenMP REQUIRED)
if (OpenMP_CXX_FOUND)
  include_directories(${CMAKE_CUR_DIR) ${OpenMP_CXX_INCLUDE_DIRS})
endif()

但是OpenMP_CXX_INCLUDE_DIRS仍然是一个空字符串,而使用include <omp.h>的代码仍然会引发错误,我该如何处理?

我尝试了message(STATUS "${OpenMP_C_INCLUDE_DIRS}"),但它仍然是一个空字符串。

英文:

I met a project which enable omp and has this line of code in CMakeLists.txt:

include_directories(${CMAKE_CUR_DIR} /usr/lib/llvm-11/include/openmp/)

and I tried to make it more general like:

find_package(OpenMP REQUIRED)
if (OpenMP_CXX_FOUND)
  include_directories(${CMAKE_CUR_DIR) ${OpenMP_CXX_INCLUDE_DIRS})
endif()

but the OpenMP_CXX_INCLUDE_DIRS is still a empty string and the code using include <omp.h> still throw an error, how can I do this?

I tried message(STATUS "${OpenMP_C_INCLUDE_DIRS}") it still a empty string.

答案1

得分: 1

include_directories 是旧的 CMake API,现在不应该使用。

这应该足以使其工作(没有测试过,基于此 SO 问题完成):

find_package(OpenMP REQUIRED)

add_executable(YourExecutable)
target_link_libraries(YourExecutable PUBLIC OpenMP::OpenMP_CXX)

由于您使用了 REQUIRED 标志,因此不需要进行 if 检查(如果找不到库,将报告错误)。

英文:

include_directories is old cmake API and is should not be used in now.

This should be enough to make it work (didn't test it, done base on this SO question):

find_package(OpenMP REQUIRED)

add_exectuable(YourExecutable)
target_link_libraries(YourExecutable PUBLIC OpenMP::OpenMP_CXX)

Since you are using REQUIRED flag the if check is not needed (error will be reported if library can't be found).

huangapple
  • 本文由 发表于 2023年7月24日 19:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定