0

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.

  • What is this `CMAKE_CUR_DIR`? Did you mean to use: https://cmake.org/cmake/help/latest/variable/CMAKE_CURRENT_SOURCE_DIR.html – Marek R Jul 24 '23 at 12:03
  • Most compilers support OpenMP by some flag, which automatically adds OpenMP include directories. That is, after the call to `find_package(OpenMP)` you need to consume not only `OpenMP_CXX_INCLUDE_DIRS` variable, but `OpenMP_CXX_FLAGS` too. It is simpler, however, to link with corresponding IMPORTED target (`OpenMP::OpenMP_CXX` for C++ language). – Tsyvarev Jul 24 '23 at 13:58

1 Answers1

1

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).

Marek R
  • 32,568
  • 6
  • 55
  • 140