FindOpenMP: Linking with OpenMP::CXX fails using Clang
Something involving linking with OpenMP::CXX using Xcode broke between v3.24.1 and v3.26.4.
How to reproduce on any (?) system:
- Make a project:
cmake_minimum_required(VERSION 3.26)
project(test)
find_package(OpenMP)
add_executable(test test.cpp)
target_compile_options(test PUBLIC -fopenmp)
target_link_libraries(test PUBLIC OpenMP::OpenMP_CXX)
(My test.cpp
is just int main() { return 0; }
)
- Run cmake and change the following line in
CMakeCache.txt
:
OpenMP_CXX_FLAGS:STRING=-Xclang -fopenmp
- Build and get the error:
[1/2] Building CXX object CMakeFiles/test.dir/test.cpp.o
FAILED: CMakeFiles/test.dir/test.cpp.o
/usr/lib64/ccache/c++ -fopenmp -Xclang -MD -MT CMakeFiles/test.dir/test.cpp.o -MF CMakeFiles/test.dir/test.cpp.o.d -o CMakeFiles/test.dir/test.cpp.o -c /home/mahrud/Projects/M2/quickfix/M2/BUILD/build/test/test.cpp
c++: error: unrecognized command-line option ‘-Xclang’
ninja: build stopped: subcommand failed.
In particular, note that the first -fopenmp
is coming from my target_compile_options
call, but the -Xclang -fopenmp
part is truncated to just -Xclang -MD ...
, which breaks the build.
Note that on macOS, using Xcode, the error becomes:
error: unknown argument: '-MD'
Also note that in this simple case I can manually remove target_compile_options
, but in practice that part is coming from a dependency has a public flag. This was noticed here: https://github.com/Macaulay2/homebrew-tap/issues/181