Reputation: 3705
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?
Upvotes: 21
Views: 31188
Reputation: 210
Izana's answer is perfect, but since Homebrew now moved every installed library into /opt/homebrew
, and the install command brew install libomp
results in the destination /opt/homebrew/opt/libomp
, one typically needs to run their CMake with a new CMAKE_PREFIX_PATH
using:
cmake -DCMAKE_PREFIX_PATH=/opt/homebrew ..
And then you probably want to manually set the include directory to make #include <omp.h>
work (on macOS):
set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp")
set(OpenMP_C_LIB_NAMES omp)
set(OpenMP_omp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_C_INCLUDE_DIR "${CMAKE_PREFIX_PATH}/opt/libomp/include") # add this!
find_package(OpenMP REQUIRED)
One might need to specify the link directory for the target as well:
target_link_directories(foo PRIVATE ${CMAKE_PREFIX_PATH}/opt/libomp/lib) # add this!
Upvotes: 0
Reputation: 325
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.
set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/5.0.1/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/5.0.1/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/Cellar/llvm/5.0.1/lib")
set(OPENMP_INCLUDES "/usr/local/Cellar/llvm/5.0.1/include")
OPTION (USE_OpenMP "Use OpenMP to enable <omp.h>" ON)
# Find OpenMP
if(APPLE AND USE_OpenMP)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(OpenMP_C "${CMAKE_C_COMPILER}")
set(OpenMP_C_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_C_LIB_NAMES})
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
set(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_CXX_LIB_NAMES})
endif()
endif()
if(USE_OpenMP)
find_package(OpenMP REQUIRED)
endif(USE_OpenMP)
if (OPENMP_FOUND)
include_directories("${OPENMP_INCLUDES}")
link_directories("${OPENMP_LIBRARIES}")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
# set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif(OPENMP_FOUND)
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).
Upvotes: 19
Reputation: 1
MacOS Ventura
cmake: /usr/local/Cellar/cmake/3.25.1 OpenMP: /usr/local/Cellar/libomp/15.0.7 llvm: /usr/local/Cellar/llvm/15.0.7_1
above solution worked after setting compiler directories and libomp directories to the correct values
with the change:
set(OpenMP_C_FLAGS "-fopenmp=libomp")
for both the clang and clangxx sections
Upvotes: 0
Reputation: 90276
With recent versions of CMake (3.18, didn't work with 3.14) and a fresh installation of MacOS (with developer CL tools installed, of course), brew install libomp
was the only action needed to make things work.
Upvotes: 3
Reputation: 3105
Maybe it's a CMake version thing, I come up with a slightly different solution with Franzi's.
I also use brew install libomp
on my machine. It seems like OpenMP_CXX_FLAGS
is used for compiling project source code instead of compiling the omp (the flag is stored in the omp target and will be populated by command target_link_libraries
).
Besides that, OpenMP_CXX_LIB_NAMES
shouldn't have prefix lib
because it will cause an error like -llibomp
not found, where -lomp
should be used instead.
I also noticed that CMAKE_C_COMPILER_ID
is AppleClang
instead of Clang
if I put project(playground)
after cmake_minimum_required
. In reverse, it's Clang
, quite annoying and I don't know why.
Xpreprocessor
used here is because apple clang doesn't ship with OpenMP and this flag tells the compiler to look for pragma (preprocessor expansion) elsewhere. In our case, it's the header files in the include path where the libomp is installed.
cmake_minimum_required(VERSION 3.12)
project(playground)
if(APPLE)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
if(CMAKE_C_COMPILER_ID MATCHES "Clang\$")
set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp")
set(OpenMP_C_LIB_NAMES "omp")
set(OpenMP_omp_LIBRARY omp)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang\$")
set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp")
set(OpenMP_CXX_LIB_NAMES "omp")
set(OpenMP_omp_LIBRARY omp)
endif()
endif()
find_package(OpenMP REQUIRED)
add_executable(helloworld helloworld.cxx)
target_link_libraries(helloworld PRIVATE OpenMP::OpenMP_CXX)
Here's my helloworld
#include <cstdio>
#include <thread>
#include <sstream>
int main(void)
{
#pragma omp parallel
{
std::stringstream ss;
ss << std::this_thread::get_id();
printf("%s, Hello, world.\n", ss.str().c_str());
}
return 0;
}
output is,
0x700002dc8000, Hello, world.
0x10a17d5c0, Hello, world.
0x7000045d1000, Hello, world.
0x7000055d7000, Hello, world.
0x700005dda000, Hello, world.
0x7000035cb000, Hello, world.
0x7000065dd000, Hello, world.
0x700003dce000, Hello, world.
0x700007de6000, Hello, world.
0x700004dd4000, Hello, world.
0x7000075e3000, Hello, world.
0x700006de0000, Hello, world.
Upvotes: 10
Reputation: 131
Apparently, case is important. For an unrelated project I can make it work with
find_package ( OPENMP REQUIRED )
This didn't work:
find_package ( OpenMP REQUIRED )
With that directive, no need for setting all the other flags by hand. cmake 3.13.2, clang-1000.11.45.5 (High Sierra)
Upvotes: 11