Friday, September 2, 2022

[SOLVED] CMake how to determine the default C++ Standard?

Issue

I can set the variable CMAKE_CXX_STANDARD like this

set(CMAKE_CXX_STANDARD 17)

but I can't print it like this

message("This is the C++ Standard we are using: ${CMAKE_CXX_STANDARD}")

I guess the variable CMAKE_CXX_STANDARD doesn't exist by default unless explicitly specified.

In such case, how is the default C++ Standard determined?


Solution

I guess the variable CMAKE_CXX_STANDARD doesn't exist by default unless explicitly specified.

Correct. If you need a specific C++ version you should specify.

In such case, how is the default C++ Standard determined?

If you don't specify it depends on the compiler. As an example new versions of MSVC for example default to C++14. As they don't want to maintain C++11 / C++98.

Not specifying the C++ standard is equivalent to not specifying other compiler flags. IE you are fine with the compiler provided defaults. Similar to how if you don't specify RTTI most C++ compilers will assume you want it enabled.

There are some projects that strive to work with whatever the compiler will give them, and a result won't specify the standard. This depends on what you want to support as a developer.

In most cases though it's a good idea to specify the C++ standard for your project.



Answered By - jpr42
Answer Checked By - Willingham (WPSolving Volunteer)