Setting CMAKE_CXX_STANDARD to various values

11,868

Solution 1

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.

Solution 2

In CMake world, first invocation of cmake differs from later ones (from the view of setting options in command line):

  1. 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.
  2. 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.

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")
Share:
11,868

Related videos on Youtube

Barry
Author by

Barry

I like c++. I am also somewhat obsessed with SO. Users I subscribe to, in no particular order: Jonathan Wakely, T.C., Columbo, Yakk, dyp Various badge milestones: 1st to receive the gold C++20 badge 1st to receive the gold C++17 badge 2nd to receive the gold language-lawyer badge 2nd to receive the gold C++14 badge 4th to receive the silver C++14 badge 8th to receive the gold templates badge 11th to receive the bronze C++14 badge 29th to receive the gold C++11 badge 31st to receive the silver templates badge 256th to receive the gold C++ badge, or 0x0100th if you prefer

Updated on September 16, 2022

Comments

  • Barry
    Barry about 1 year

    I have a C++ library, that's intended to be usable across several compiler versions and several C++ standards. I have tests for this library - and I need to ensure that these tests pass for this matrix of compilers/versions that I wish to support.

    I can provide -DCMAKE_CXX_STANDARD=xx (for 11, 14, 17) on the command line, and this seems to work fine. How do I provide a default value for this field? I would like that, if not provided by the user, that default is 11. It seems that when I do:

    $ CXX=/path/to/gcc-7 cmake .. -DCMAKE_CXX_STANDARD=17
    # does stuff, CMAKE_CXX_STANDARD is 17
    
    $ CXX=/path/to/gcc-7 cmake .. 
    # does stuff, CMAKE_CXX_STANDARD is still 17
    

    which is counterintuitive to me. Is there a way to make the latter use the desired default value of 11?

    Also, if I just rerun cmake with a different value in the same build directory, would that be enough to trigger a rebuild or would I need a new build directory?

    • Barry
      Barry
      @JesperJuhl At least that last part I found how to fix! (set(CMAKE_CXX_EXTENSIONS OFF))
    • Jesper Juhl
      Jesper Juhl
      Note that cmake caches some settings and don't update them if you just run cmake again. For some things you have to clear the build dir (or delete the CMakeCache.txt file and re-run cmake). Aldo note that when setting CMAKE_CXX_STANDARD, cmake (in its infinite wisdom - not) sets the "gnu-xx" version that allows compiler extensions (when using gcc at least) - that's not always what you want (I blame the cmake guys for that poor desition).
  • TonyParker
    TonyParker almost 5 years
    @usr1234569 In option() command, 1st parameter is name of variable, 3rd is its value. what is 2nd parameter ("C++ standard") ?
  • usr1234567
    usr1234567 over 4 years
    Its a help text, to indicate what's the purpose of the variable. For example the text is shown in the cmake gui.
  • Rimas
    Rimas over 3 years
    The first approach seems doesn't work. When Barry_CXX_STANDARD is not specified in command line, I got OFF value instead of 11. Documentation states, what option is for ON/OFF values only: cmake.org/cmake/help/latest/command/option.html