How to enable C++17 in CMake

C++Visual StudioCmakeC++17

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

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)

Solution 2 - C++

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.

Solution 3 - C++

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..
            )

Solution 4 - C++

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

Solution 5 - C++

when using vs2019

set(CMAKE_CXX_STANDARD 17)

Solution 6 - C++

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)

Solution 7 - C++

Bash command line in CMake flags:

-DCMAKE_CXX_STANDARD=17 \
-DCMAKE_CXX_STANDARD_REQUIRED=ON \
-DCMAKE_CXX_EXTENSIONS=OFF \

Solution 8 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMiPView Question on Stackoverflow
Solution 1 - C++nboutView Answer on Stackoverflow
Solution 2 - C++Some programmer dudeView Answer on Stackoverflow
Solution 3 - C++loneraverView Answer on Stackoverflow
Solution 4 - C++The Quantum PhysicistView Answer on Stackoverflow
Solution 5 - C++tesla1060View Answer on Stackoverflow
Solution 6 - C++Mike LeeView Answer on Stackoverflow
Solution 7 - C++Danoli3View Answer on Stackoverflow
Solution 8 - C++SajalView Answer on Stackoverflow