How can I propagate CXX_STANDARD and related properties to dependent targets?

474 views Asked by At

When you set a CMake target's property, you can make it PUBLIC, INTERFACE or PUBLIC. Yet - the CMake manual page on the CXX_STANDARD property does not indicate the ability to specify one of these. Specifically, suppose I have:

set_target_properties(mylib PROPERTIES 
        CXX_STANDARD 11
        CXX_STANDARD_REQUIRED YES
        CXX_EXTENSIONS NO
)

Is it really unavailable? And if so, why?

(Note: This question applies just the same to C or any language which CMake supports this way.)

1

There are 1 answers

0
einpoklum On

As @Mizux points out in a comment, it seems you can't - at the moment - make these CXX_-prefixed options propagate to dependent targets: Propagating properties are named INTERFACE_ + the original property name - and there are no properties with prefix INTERFACE_CXX_ in the master list of target properties as of October 2020.

I don't know why this is the case.

However - one can obtain the effect of INTERFACE or PUBLIC on these properties, to some extent, by using the target_compile_features() command (or is it a macro? I always get those mixed up), with one of the features: cxx_std_98, cxx_std_11 etc. Thus, for example:

target_compile_features(mylib PUBLIC cxx_std_11)

but that's still not the exact PUBLIC equivalent of OP's command in the question: This doesn't prevent the availability of GNU extensions. So, it's a half-solution - and I don't like it anyway because the syntax in the question is nicer.