Setting CMAKE_CXX_STANDARD to various values
You can use the [option][1]
command to let the user choose and give a default value yourself:
option(Barry_CXX_STANDARD "C++ standard" 11)
set(CMAKE_CXX_STANDARD Barry_CXX_STANDARD)
The variable name Barry_CXX_STANDARD
indicated that it is specific to your project and should be the same prefix as all project-specific variables are named.
The downside of this approach is, that experienced CMake users would be surprised and set CMAKE_CXX_STANDARD
directly.
Another approach is to check whether the variable is set.
if(NOT "${CMAKE_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD 11)
endif()
If CMake provides already a variable, I would use the second approach. If it is only your project-specific variable, the first one is better.
In any case, if you want to change the value you have to delete the CMakeCache.txt
in your build directory. Otherwise the caching hides the change.
In CMake world, first invocation of cmake
differs from later ones (from the view of setting options in command line):
First invocation:
- If option's value is given in command line(
-DOPTION=VALUE
), this value is used. - If option's value is not given in command line, default value is used.
- If option's value is given in command line(
Later invocations:
- If option's value is given in command line(
-DOPTION=VALUE
), this value is used. - If option's value is not given in command line, previous value is used.
- If option is unset (
-UOPTION
), default value is used.
- If option's value is given in command line(
Thus, having one cmake
invocation already done, one may:
modify some of options and leave other unchanged
For doing that, pass modified options' values with
-D
.reset some of options and leave other unchanged
For doing that, pass reset options' values with
-U
.set some options, and reset others to default values
For doing that, make a clean rebuild by removing
CMakeCache.txt
from build directory or removing all files from build directory.
For assign default value for an option in the project, use common CACHE variables:
set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard to be used")