1
0
mirror of synced 2024-12-23 05:00:01 +01:00

36 lines
1.4 KiB
Markdown
Raw Normal View History

2018-03-01 22:01:04 +01:00
# OpenMP
[OpenMP] support was drastically improved in CMake 3.9+. The Modern(TM) way to add OpenMP to a target is:
```cmake
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(MyTarget PUBLIC OpenMP::OpenMP_CXX)
endif()
```
2018-08-20 10:21:41 +02:00
This not only is cleaner than the old method, it will also correctly set the library link line differently from the compile line if needed. In CMake 3.12+, this will even support OpenMP on macOS (if the library is available, such as with `brew install libomp`). However, if you need to support older CMake, the following works on CMake 3.1+:
2018-03-01 22:01:04 +01:00
```cmake
# For CMake < 3.9, we need to make the target ourselves
if(NOT TARGET OpenMP::OpenMP_CXX)
2018-04-05 23:25:03 +02:00
find_package(Threads REQUIRED)
2018-03-01 22:01:04 +01:00
add_library(OpenMP::OpenMP_CXX IMPORTED INTERFACE)
set_property(TARGET OpenMP::OpenMP_CXX
PROPERTY INTERFACE_COMPILE_OPTIONS ${OpenMP_CXX_FLAGS})
# Only works if the same flag is passed to the linker; use CMake 3.9+ otherwise (Intel, AppleClang)
set_property(TARGET OpenMP::OpenMP_CXX
2018-04-05 23:25:03 +02:00
PROPERTY INTERFACE_LINK_LIBRARIES ${OpenMP_CXX_FLAGS} Threads::Threads)
2018-03-01 22:01:04 +01:00
endif()
target_link_libraries(MyTarget PUBLIC OpenMP::OpenMP_CXX)
```
2024-08-15 07:04:35 +00:00
:::{danger}
CMake < 3.4 has a bug in the Threads package that requires you to have the `C` language enabled.
:::
2018-04-05 23:25:03 +02:00
2022-06-02 14:30:14 -04:00
[openmp]: https://cmake.org/cmake/help/latest/module/FindOpenMP.html