I am using the newest CMake (3.9.3) from Homebrew along with LLVM 5.0.0 also from Brew, because Clang here has OpenMP support.
This worked in CMake 3.8.2 with LLVM 5.
In my CMakeLists.txt
I have
find_package( OpenMP )
and later I want to do
if( OpenMP_CXX_FOUND )
However CMake doesn't seem to pick up on the find_package
directive.
I run CMake with
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -DUSE_WERROR=ON
where I have checked that clang
and clang++
points correctly to /usr/local/opt/llvm/bin/clang
and /usr/local/opt/llvm/bin/clang++
All I get is these two lines:
-- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES) (found version "1.0")
-- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES) (found version "1.0")
If I set OpenMP_C_FLAGS
myself (with -DOpenMP_C_FLAGS=-fopenmp=libomp
) it changes the error to
-- Could NOT find OpenMP_C (missing: OpenMP_C_LIB_NAMES) (found version "3.1")
Notice that it changes the version number, so it must be finding something, right?
What am I missing for this to work properly?
Okay, it seem that inside the FindOpenMP.cmake
supplied by CMake we do a try_compile
, which fails silently (because we do it a lot of times and most of them will fail, this makes sense). However, with Clang a -Werror
flag is supplied, which fails because of an unused command line argument. I can thus add:
if(APPLE)
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(OpenMP_C_FLAG "-fopenmp=libomp -Wno-unused-command-line-argument")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(OpenMP_CXX_FLAG "-fopenmp=libomp -Wno-unused-command-line-argument")
endif()
endif()
to my project because I know that -fopenmp=libomp
will work for this Clang.
Is this the right way of doing it?
The message basically tells you that you have to provide the path to the libraries and the names of the libraries. The following example should fix your problem (see also find_package(OpenMP)). Note that I use the brew installation using the command "brew install llvm". The first four lines are just for completeness.
You might want to set e.g.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
such that the linker automatically detects the appropriate pthread library (see pthread and wiki).Apparently, case is important. For an unrelated project I can make it work with
This didn't work:
With that directive, no need for setting all the other flags by hand. cmake 3.13.2, clang-1000.11.45.5 (High Sierra)