Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting CMAKE_CXX_STANDARD to various values

Tags:

c++

cmake

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?

like image 344
Barry Avatar asked Dec 29 '17 17:12

Barry


2 Answers

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.

like image 111
usr1234567 Avatar answered Sep 29 '22 00:09

usr1234567


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")
like image 35
Tsyvarev Avatar answered Sep 29 '22 00:09

Tsyvarev