FindOpenMP.cmake does not correctly populate link line
I am working on a project where a CMake built library is utilizing OpenMP and has this in the corresponding CMakeLists.txt:
find_package(OpenMP QUIET)
if (OpenMP_CXX_FOUND)
target_link_libraries(${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX)
endif ()
On Linux (openSUSE Tumbleweed) with cmake-full-3.19.2-1.1.x86_64, linking the final application against that library fails because the linker does not get the relevant -fopenmp
flag. Dumping variables and properties shows, the following get set up by FindOpenMP.cmake:
OpenMP::OpenMP_CXX BINARY_DIR = <redacted>
OpenMP::OpenMP_CXX IMPORTED = TRUE
OpenMP::OpenMP_CXX INTERFACE_COMPILE_OPTIONS = $<$<COMPILE_LANGUAGE:CXX>:-fopenmp>
OpenMP::OpenMP_CXX NAME = OpenMP::OpenMP_CXX
OpenMP::OpenMP_CXX RULE_LAUNCH_COMPILE = /usr/bin/ccache
OpenMP::OpenMP_CXX SOURCE_DIR = <redacted>
OpenMP::OpenMP_CXX TYPE = INTERFACE_LIBRARY
OpenMP_CXX_FIND_QUIETLY=1
OpenMP_CXX_FLAGS=-fopenmp
OpenMP_CXX_FOUND=TRUE
OpenMP_CXX_INCLUDE_DIRS=
OpenMP_CXX_LIBRARIES=
OpenMP_CXX_LIB_NAMES=
OpenMP_CXX_SPEC_DATE=201511
OpenMP_CXX_VERSION=4.5
OpenMP_CXX_VERSION_MAJOR=4
OpenMP_CXX_VERSION_MINOR=5
So, obviously because of the empty OpenMP_CXX_LIBRARIES
, the linker will not get any parameters necessary to link the OpenMP libs.
I am using this rather unpleasant fix-up which however triggers linker warnings on Windows:
if (OpenMP_CXX_FOUND)
target_link_libraries(${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX)
target_link_options(${PROJECT_NAME} PUBLIC ${OpenMP_CXX_FLAGS})
endif ()
So, in short, I think, FindOpenMP.cmake needs to set up OpenMP_CXX_LIBRARIES
as -fopenmp
when using "implict linking" (is this the right term?).