I try to understand why setting the C++ standard properties on an interface library doesn’t work. I can’t find this documented or properly explained anywhere. CMake doesn’t seem to issue any warnings or errors.
add_library(CMakeTestLib INTERFACE)
set_target_properties(CMakeTestLib
PROPERTIES
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
# or
set_property(TARGET CMakeTestLib PROPERTY CXX_STANDARD 23)
set_property(TARGET CMakeTestLib PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET CMakeTestLib PROPERTY CXX_EXTENSIONS OFF)
If I set the standard version via compile features, why aren’t there “compile features” to also turn off compiler extensions?:
Add an Interface Library target that may specify usage requirements for dependents but does not compile sources and does not produce a library artifact on disk.
An interface library with no source files is not included as a target in the generated buildsystem. However, it may have properties set on it and it may be installed and exported. Typically, INTERFACE_* properties are populated on an interface target using the commands:
If I understand the above correctly I can use an interface library to define all my properties in a single consistent way and to apply them to all targets that link to the interface library transitively.
Is that so or am I “holding it wrongly”?
I’ve juste made a test and replaced CXX_EXTENSIONS without any change in behavior. As stated in previous link, I’m beginning to suspect that this property is useless for headers-only libraries.
the CXX_STANDARD property itself only affects how sources within that target are built. Since interface libraries have no sources it doesn’t matter. Transitive usage requirements are of the form INTERFACE_... where ... is the property on the dependent targets to extend. This only works for a specific set of properties, and CXX_STANDARD is not among them. The intended way to propagate C++ language requirements is with INTERFACE_COMPILE_FEATURES.