Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

target_compile_options() for only C++ files?

Tags:

Is it possible to use target_compile_options() for only C++ files? I'd like to use it for a target that is uses as a dependency for other applications so that the library can propagate its compiler flags to those apps. However, there are certain flags, such as -std=c++14, that cause the build to fail if they are used with C or ObjC files.

I've read that I should CXX_FLAGS instead to only add those flags to C++ files, however this won't (automatically) propagate through cmake's packages system.

like image 896
rich.e Avatar asked Jul 25 '16 23:07

rich.e


1 Answers

Solution

You can do this with generator expressions:

target_compile_options(MyLib PUBLIC $<$<COMPILE_LANGUAGE:CXX>:-std=c++14>) 

Alternative

But the more platform independent way of doing it in this particular case would be to use target_compile_features(). I'm not sure which compiler feature you're using, so the following is only an example:

target_compile_features(MyLib PUBLIC cxx_explicit_conversions) 
like image 63
Florian Avatar answered Sep 23 '22 08:09

Florian