I have a library target atc
which is fully written in C++ and therefore compiled with CMAKE_CXX_COMPILER
. atc
provides a pure C interface which I want to test using the example executable example-c
. However, atc
uses OpenMP internally so I also need to link example-c
using the correct flags. Right now I'm using this CMake code:
add_executable(example-c src/executables/example.c)
target_link_libraries(example-c atc)
if(OpenMP_FOUND)
target_link_libraries(example-c OpenMP::OpenMP_C)
endif()
[...]
As far as I know, using the target OpenMP::OpenMP_<lang>
is the modern way of doing things, so I'm trying to use this. However, problems arise when CMAKE_CXX_COMPILER
is clang
and CMAKE_C_COMPILER
is gcc
(default):
cc: error: unrecognized command line option ‘-fopenmp=libomp’; did you mean ‘-fopenmp-simd’?
Checking the generated command, it turns out the Makefile is trying to compile (not link yet, although I had problems with this earlier too) example-c
with gcc
using the flags -fopenmp=libomp -fopenmp
, with the first option not being a valid gcc
option. This probably happens because some earlier targets are C++ targets and as such are compiled using clang
and linked with the OpenMP::OpenMP_CXX
target.
Is there a way to make sure that when I link with a target of language <lang>
with OpenMP::OpenMP_<lang>
, CMake sets everything up specifically for CMAKE_<lang>_COMPILER
, without interfering with the other targets in the project? For your information, I'm using CMake 3.10.2.
CMAKE_CXX_COMPILER
isclang
andCMAKE_C_COMPILER
isgcc
" - yes, having in a single CMake project C and C++ compilers from different "vendors" is a direct way to problems. I am almost sure that OpenMP is not the only CMake module which fails to live in such settings. I would suggest to separate builds which usegcc
from ones which useclang
. (You could perfectly link libraries created in one build with libraries in another).