I'm trying to add the /std:c++17
compiler flag to VS2017 with CMake. I'm using the "modern" cross-platform way so far:
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) # -std=c++11 instead of -std=gnu++11 set(MY_CXX_COMPILE_FEATURES cxx_generic_lambdas cxx_range_for cxx_strong_enums) add_library(mylib INTERFACE) target_compile_features(mylib INTERFACE ${MY_CXX_COMPILE_FEATURES})
This adds /std:c++14
in VS2017 (which might be the default anyway?). However I'm having trouble switching this to C++17 (i.e. having it add /std:c++17
). If I just add it manually, I get the not-so-nice warning because both flags are present:
1>cl : Command line warning D9025: overriding '/std:c++14' with '/std:c++17'
I've tried set(CMAKE_CXX_STANDARD 17)
but it has no effect, in fact the CMake documentation mentions that CMAKE_CXX_STANDARD
has no effect on VS anyway.
As for adding a C++17 feature to target_compile_features
, it doesn't seem like there are any yet (even in CMake-3.9.0-rc5), and even if there were, I'm specifically only using std::optional
from C++17, and there's no target_compile_features
flags for library features like std::optional
.
So my question is, what's the best (or least ugly) way to do this with CMake? And in a way so it'll also work for gcc and clang? I'm happy to use a very recent CMake version (3.8 or 3.9). I prefer it to be "nice" and not manually looping through CXX_COMPILE_FLAGS and removing the string "/std:c++14" or some hack like that.
(Edit: It can also be the VS/std:c++latest
switch - whichever is possible. Both work for the purpose.)
Add to Additional options in Project Settings: /std:c++latest to enable latest features - currently C++17 as of VS2017, VS2015 Update 3. /permissive- will disable non-standard C++ extensions and will enable standard conformance in VS2017.
Support for C11 and C17 standards is available in Visual Studio 2019 version 16.8 and later. Support requires an updated Universal C Runtime (UCRT) and Windows SDK version to work properly with the conforming preprocessor ( /Zc:preprocessor ).
Select the Configuration Properties > C/C++ > Language property page. In C++ Language Standard (or for C, C Language Standard), choose the language standard to support from the dropdown control, then choose OK or Apply to save your changes.
C++20 Coroutines are now supported under /std:c++latest (or /std:c++20 starting in Visual Studio 2019 version 16.11) and the <coroutine> header.
Turning my comment into an answer
The CMake team is working on it for VS2017 (as for July 2017, for upcoming CMake version 3.10):
CMake: MSVC standard version switches
Those flags seem to be rather new switches (as related to the date of this question):
VS 2017 15.3 preview now supports /std:c++17
So for Visual Studio you have to "manually" replace or append the compiler switches until CMake officially does support it.
Here is a code snippet that I've tested for std:c++latest
(which is already supported e.g. in my CMake 3.8.0 version):
if (MSVC_VERSION GREATER_EQUAL "1900") include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported) if (_cpp_latest_flag_supported) add_compile_options("/std:c++latest") endif() endif()
For CLang and GNU the support was merged into the main source code branch begin of 2017 and is part of CMake version 3.8 and above:
CMake: Features: Add support for C++ 17 language standard
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With