GCC 4.x doesn't accept the --std=c++14
switch for C++14 code - it takes --std=c++1y
instead. Later versions take --std=c++1z
but (probably) not --std=c++17
which has not been set yet (writing this in 2016). Perhaps there are similar issues with C++11.
Does CMake have some facility (perhaps as a module) to pass the correct switch according to the GCC version?
Check if the compiler supports the flags? Perhaps something like
When wanting to specify a particular C++ version, the recommended way to do this with CMake 3.1 and later is to use the
CXX_STANDARD
,CXX_STANDARD_REQUIRED
andCXX_EXTENSIONS
target properties, or their variable equivalents to specify target defaults. Full details can be found here, but the short version goes something like this:CMake should then select the appropriate compiler flag for the requested C++ standard based on what the compiler supports, or error out if it doesn't support the requested standard.
It should also be noted that CMake may upgrade the target to use a later language standard than the one specified by its
CXX_STANDARD
target property. The use of compile feature requirements (as mentioned in @FlorianWolters answer) can raise the language standard requirement. In fact, CMake will always pick the stronger language requirement specified by either theCXX_STANDARD
target property or the compile feature requirements set on the target. Note also that the CMake documentation as of 3.10.1 does not accurately reflect the wayCXX_EXTENSIONS
interacts with compile features, asCXX_EXTENSIONS
only takes effect ifCXX_STANDARD
is also specified for most common compilers (since they are specified together with the one compiler flag).Modern CMake code should use the
target_compile_features
command to request a specific C++ standard. This can be specified as build requirement only (PRIVATE
), usage requirement only (INTERFACE
) or build and usage requirement (PUBLIC
).Example:
Refer to the section Requiring Language Standards in the official CMake documentation for cmake-compile-features to learn more.