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,
INTERFACE should be used instead of
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 )
set(CMAKE_C_STANDARD 99) set(CMAKE_CXX_STANDARD 11)
These will add the needed compile options on targets (e.g.
-std=c++11 for gcc).
The version can be made a requirement by setting to
ON the variables
The variables must be set before target creation. The version can also be specified per-target:
set_target_properties(foo PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON )