128

I'm using VS 15.3, which supports integrated CMake 3.8. How can I target C++17 without writing flags for each specific compilers? My current global settings don't work:

# https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# expected behaviour
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest")

I expected CMake to add "/std:c++lastest" or equivalents when generating VS solution files, but no c++17 flags was found, resulted in compiler error:

C1189 #error: class template optional is only available with C++17.
evandrix
  • 6,041
  • 4
  • 27
  • 38
MiP
  • 5,846
  • 3
  • 26
  • 41

8 Answers8

80

Modern CMake propose an interface for this purpose target_compile_features. Documentation is here: Requiring Language Standards

Use it like this:

target_compile_features(${TARGET_NAME} PRIVATE cxx_std_17)

vitaut
  • 49,672
  • 25
  • 199
  • 336
nbout
  • 1,159
  • 8
  • 12
  • @vitaut Can you explain why you rename ${PROJECT_NAME} into ${TARGET_NAME} ? – nbout Jul 04 '19 at 15:49
  • Project has a different meaning in CMake: https://cmake.org/cmake/help/v3.0/command/project.html. `target_compile_features` applies to targets. – vitaut Jul 04 '19 at 15:56
  • 2
    So I changed it for clarity. Feel free to revert. – vitaut Jul 04 '19 at 15:57
  • 2
    I wish accepted answers automatically changed as new language features came out... This is the right answer. the ones above are all very outdated. – tjwrona1992 Sep 04 '19 at 02:09
  • With CMake 3.13 and Visual 2017, I tought this modern command didn't add the `/std:c++17` flag but the `set(CMAKE_CXX_STANDARD 17)` did... but it was just a bad keyword `INTERFACE` to `PUBLIC` solved it. – Sandburg Jan 14 '21 at 09:41
  • 2
    In CMake, `PUBLIC` (for everyone) = `INTERFACE` (for the other) + `PRIVATE` (for me) – Sandburg Jan 14 '21 at 09:47
48

Your approach is the correct one, but it will not work for MSVC on versions of CMake prior to 3.10.

From the CMake 3.9 documentation:

For compilers that have no notion of a standard level, such as MSVC, this has no effect.

In short, CMake haven't been updated to accommodate for the standard flags added to VC++ 2017.

You have to detect if VC++ 2017 (or later) is used and add the corresponding flags yourself for now.


In CMake 3.10 (and later) this have been fixed for newer version of VC++. See the 3.10 documentation.

fuzzyTew
  • 3,511
  • 29
  • 24
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 4
    The newer CMake 3.10 (and later) documentation says it works for Visual Studio 2015 update 3 or newer. Only Visual Studio versions before 2015 Update 3 are not supported. The link above actually goes to the latest documentation, which reflects this. – Marcus10110 Nov 06 '17 at 17:57
  • 1
    @Marcus10110 Correct, changed the link to point to the 3.9 documentation. And added note about it being fixed for newer versions. Thanks. – Some programmer dude Nov 06 '17 at 17:59
  • 4
    A (maybe) pedantic comment: to make MSVC behave as a conforming compiler, you also have to use the `/Zc:__cplusplus` flag because otherwise the `__cplusplus` macro is not set to the right value. A certainly pedantic comment is that I consider this a bug in cmake: when I ask for C++11, C+14, C++17 I want cmake to set *all* flags that are needed to make the compiler conform, and that includes the value of the `__cplusplus` macro. – tobi_s May 23 '19 at 09:18
32

In modern CMake, I've found it best to assign CXX standards at the target level instead of global variable level and use the built in properties (seen here: https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html) to keep it compiler agnostic.

For Example:

set_target_properties(FooTarget PROPERTIES
            CXX_STANDARD 17
            CXX_EXTENSIONS OFF
            etc..
            )
loneraver
  • 1,282
  • 2
  • 15
  • 22
  • Per the documentation, CXX_STANDARD_REQUIRED is a Boolean. https://cmake.org/cmake/help/v3.10/prop_tgt/CXX_STANDARD_REQUIRED.html – Brent Jul 06 '18 at 16:06
  • 5
    What does a "modern" CMake mean exactly? – ar2015 Aug 11 '18 at 13:31
  • 22
    It seems every cmake feature gets outdated immediately and the Internet is filled with outdated questions and anwsers. – zwcloud Jul 30 '19 at 03:47
  • "Modern CMake" is described in Craig Scott's book. There have been a number of rethinks along the way which has led to the scripts being more focussed around abstracted targets as opposed to shoehorning compiler flags etc. Check out his book here: https://crascit.com/professional-cmake/ – Den-Jason Oct 29 '20 at 11:53
30

You can keep that set(CMAKE_CXX_STANDARD 17) for other compilers, like Clang and GCC. But for Visual Studio, it's useless.

If CMake still doesn't support this, you can do the following for Visual Studio:

if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
endif(MSVC)

EDIT: As the question title doesn't mention the compiler, let me add that for gcc, clang and similar compilers, this is the command to enable C++17:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • 3
    [Recent VS changes](https://learn.microsoft.com/en-us/cpp/what-s-new-for-visual-cpp-in-visual-studio) allow to set standard using `/std:c++14`, `/std:c++17` and `/std:c++latest` flags. – user7860670 Aug 15 '17 at 07:51
  • 2
    In my case it's the opposite. I'm using CMake 3.19.1, VS2019 and gcc 7.5.0. `set(CMAKE_CXX_STANDARD 17)` is enough for VS2019, but gcc requilres `target_compile_features(${TARGET_NAME} PRIVATE cxx_std_17)`. Even a explicit `add_compile_options("-std=gnu++17")` is not enough. – olepinto Jan 11 '21 at 12:20
17

when using vs2019

set(CMAKE_CXX_STANDARD 17)
tesla1060
  • 2,621
  • 6
  • 31
  • 43
4

Bash command line in CMake flags:

-DCMAKE_CXX_STANDARD=17 \
-DCMAKE_CXX_STANDARD_REQUIRED=ON \
-DCMAKE_CXX_EXTENSIONS=OFF \
Danoli3
  • 3,203
  • 3
  • 24
  • 35
3

Based on my test, the following code will enable VS2019 to select /std:c++latest, otherwise for other platforms it will set the C++ version to C++17. I have tested this with emscripten, raspbian, and windows.

if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest")
else(MSVC)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
endif(MSVC)
Mike Lee
  • 2,440
  • 26
  • 12
  • 2
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Dec 30 '20 at 08:55
  • Note that the statement see slightly wrong. According to the [cmake document](https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html), the `cxx_std_17` feature means _at least C++17_. So in effect it is similar to the `/std:c++latest` except that it makes sure that you do not compile with an older version (and if a newer version is not available, cmake fails). – Alexis Wilke Jul 17 '22 at 17:35
2

You can also use target_compile_options to set /std:c++latest flag for Visual Studio 2019

if (MSVC_VERSION GREATER_EQUAL "1900")
    include(CheckCXXCompilerFlag)
    CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
    if (_cpp_latest_flag_supported)
        target_compile_options(${TARGET_NAME} PRIVATE "/std:c++latest")
    endif()
endif()

Replace ${TARGET_NAME} with the actual target name.

Sajal
  • 1,783
  • 1
  • 17
  • 21