104

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++lastest")

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.
MiP
  • 5,126
  • 3
  • 25
  • 39

8 Answers8

60

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
  • 43,200
  • 23
  • 168
  • 291
nbout
  • 917
  • 6
  • 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
  • 1
    So I changed it for clarity. Feel free to revert. – vitaut Jul 04 '19 at 15:57
  • 1
    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
  • 1
    In CMake, `PUBLIC` (for everyone) = `INTERFACE` (for the other) + `PRIVATE` (for me) – Sandburg Jan 14 '21 at 09:47
44

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,105
  • 25
  • 23
Some programmer dude
  • 380,411
  • 33
  • 383
  • 585
  • 3
    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
25

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,032
  • 1
  • 13
  • 21
  • 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
  • 4
    What does a "modern" CMake mean exactly? – ar2015 Aug 11 '18 at 13:31
  • 12
    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
22

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
  • 22,970
  • 17
  • 88
  • 165
  • 2
    [Recent VS changes](https://docs.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
  • 1
    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
14

when using vs2019

set(CMAKE_CXX_STANDARD 17)
tesla1060
  • 2,345
  • 5
  • 27
  • 42
2

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
  • 1,980
  • 21
  • 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
1

Bash command line in CMake flags:

-DCMAKE_CXX_STANDARD=17 \
-DCMAKE_CXX_STANDARD_REQUIRED=ON \
-DCMAKE_CXX_EXTENSIONS=OFF \
Danoli3
  • 3,011
  • 3
  • 23
  • 32
0

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,683
  • 1
  • 15
  • 18