Required compiler features can be specified on a target using the command target_compile_features:
add_library(foo
foo.cpp
)
target_compile_features(foo
PRIVATE # scope of the feature
cxx_constexpr # list of features
)
The features must be part of CMAKE_C_COMPILE_FEATURES or CMAKE_CXX_COMPILE_FEATURES; cmake reports an error otherwise. Cmake will add any necessary flags such as -std=gnu++11
to the compile options of the target.
In the example, the features are declared PRIVATE
: the requirements will be added to the target, but not to its consumers. To automatically add the requirements to a target building against foo, PUBLIC
or INTERFACE
should be used instead of PRIVATE
:
target_compile_features(foo
PUBLIC # this time, required as public
cxx_constexpr
)
add_executable(bar
main.cpp
)
target_link_libraries(bar
foo # foo's public requirements and compile flags are added to bar
)