Target_compile_options () only for C ++ files?

Is it possible to use target_compile_options() only for C ++ files? I would like to use it for a purpose that is used as a dependency for other applications so that the library can distribute its compiler flags for these applications. However, there are certain flags, such as -std=c++14 , which lead to a build failure if they are used with C or ObjC files.

I read that instead of CXX_FLAGS I only need to add these flags to C ++ files, however it will not (automatically) propagate through the cmake package system.

+6
source share
1 answer

Decision

You can do this using generator expressions :

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

Alternative

But a more platform-independent way to do this in this particular case is to use target_compile_features() . I am not sure which compiler function you are using, so the following example:

 target_compile_features(MyLib PUBLIC cxx_explicit_conversions) 
+7
source

All Articles