How to enable /std:c++17 in VS2017 with CMake - c++

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

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

CMake versions higher than 3.10 support MSVC C++ standard switches for MSVC versions newer than 19.0.24215. If either of the version requirements are not met, then they have no effect.
The only portable approach, to ensuring your program is compiled with the correct C++ standard mode on Visual Studio, is to require at least CMake 3.10, set the target property CXX_STANDARD to your desired value and CXX_STANDARD_REQUIRED to ON.
Example usage:
set_property(TARGET my_target PROPERTY CXX_STANDARD 17)
set_property(TARGET my_target PROPERTY CXX_STANDARD_REQUIRED ON)

You can add the following line to your CmakeLists.txt file
set(GCC_COMPILE_FLAGS "${GCC_COMPILE_FLAGS} /std:c++17)

Related

CMake: add conditional compiler flags into Visual Studio project

Visual Studio allows to select either the cl compiler or the clang-cl compiler to build projects -- these are called toolsets. These two compilers have different sets of flags, and in particular different flags for disabling warnings. Flags for one compiler produces errors on the other.
This problem can be solved in Visual Studio for both compilers at the same time by defining compiler flags conditionally based on the used toolset. Official documentation for that here.
I use CMake to generate the Visual Studio projects. How can I make CMake add such conditional flags for the generated Visual Studio projects?
You can use CMAKE_CXX_COMPILER_ID and CMAKE_CXX_SIMULATE_ID with your favourite way of handling compilers (if-else or generator expressions)
Output for -T ClangCL (Visual Studio 2019):
message(STATUS ${CMAKE_CXX_COMPILER_ID}) // Clang
message(STATUS ${CMAKE_CXX_SIMULATE_ID}) // MSVC
Output with no toolkit (Visual Studio 2019):
message(STATUS ${CMAKE_CXX_COMPILER_ID}) // MSVC
message(STATUS ${CMAKE_CXX_SIMULATE_ID}) // <empty>
Essentially a more modern approach to the earlier question here, you can use an if-statement to check the compiler type, and set compile flags based on that:
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# Disable a Clang warning type.
target_compile_options(MyLib PRIVATE -Wno-unused-variable)
elseif(MSVC)
# Disable a MSVC warning type.
target_compile_options(MyLib PRIVATE /wd4101)
endif()
For putting this into a single expression, you can use CMake generator expressions (which are evaluated at the CMake buildsystem generation stage):
target_compile_options(MyLib PRIVATE
"$<IF:$<STREQUAL:${CMAKE_CXX_COMPILER_ID},Clang>,-Wno-unused-variable,/wd4101>"
)
For reference, here is a list of all of the clang warnings types.

How to strictly target C++98 in CLion

For some reason I cannot configure CLion to only use C++98 for my project
I keep getting clang-tidy for c++11 instead of c++98 (How do I set it to C++98)
I do not get any red lines or compiler errors when I use syntax for C++ versions greater than C++98 (How do I enable strict enforcement of C++98)
When I set the IDE to C++98 I do not want the IDE to let me use any standard greater than C++98 (How do I do this?)
Does anyone know how to enforce this standard in CLion? My guess is to configure the cmake file to C++98. Not sure if I'm doing it right though...
My CMakeLists.txt is shown below
cmake_minimum_required(VERSION 3.10)
project(myProject)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c98")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98")
set(CMAKE_CXX_STANDARD 98)
add_executable(myProject main.cpp Myclass.cpp Myclass.h)
add_definitions(-std=c++98)
p.s I do not have the option to work in any other version than C++98 and lower due to hardware constraints.
I have had similar problem and setting standard for a target worked, it would be something like
set_property(TARGET myProject PROPERTY CXX_STANDARD 98)
for you

Enabling support for C++17 in CMake for Visual Studio

I have a simple CMake (3.9.4) project with 3 libraries.
Basic library is an INTERFACE one (header only). It's using some features from C++17 that are present in Visual Studio (like constexpr if). Obviously I try to set a target property so that it propagated to dependent libraries.
I've tried:
target_compile_features(my_lib INTERFACE cxx_std_17)
But nothing changed.
Second attempt was:
set_target_properties(my_lib PROPERITES CXX_STANDARD 17)
But I get:
CMake Error at cpp/CMakeLists.txt:20 (set_target_properties):
INTERFACE_LIBRARY targets may only have whitelisted properties. The
property "CXX_STANDARD" is not allowed.
Finally I ended up with:
target_compile_options(bit INTERFACE /std:c++17)
which works fine. Is this a correct solution? Taking a look at all the compile features I believe there should be something better I can do. Also that forces me to wrap the above command in some kind of if(MSVC) ... endif() shenanigans.
CMake versions higher than 3.10 support MSVC C++ standard switches, but on earlier versions they have no effect.
The only portable approach, to ensuring your program is compiled with the correct C++ standard mode on Visual Studio, is to require at least CMake 3.10, set the target property CXX_STANDARD to your desired value and CXX_STANDARD_REQUIRED to ON.
Example usage:
set_property(TARGET my_target PROPERTY CXX_STANDARD 17)
set_property(TARGET my_target PROPERTY CXX_STANDARD_REQUIRED ON)
Note: Currently CXX_STANDARD documentation for CMake 3.10 incorrectly states that it has no effect on MSVC. There's an issue tracking this here.

Compiling C++17 using CLion, CMake and the VS2017 compiler

When creating a new CLion project and selecting the C++17 language standard the C++17 code will not compile.
The initial CMakeLists.txt file is as follows:
cmake_minimum_required(VERSION 3.8)
project(optional2)
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES main.cpp)
add_executable(optional2 ${SOURCE_FILES})
To compile the C++17 code I had to add the following line to the CMakeLists.txt file:
add_compile_options(/std:c++latest)
Is this the correct way (and/or only way?) to add this compile option in CMAKE / CLion?
The C++ compiler in use is the vs2017 cl.exe with nmake.exe on a Windows 10 workstation running CLion 2017.2.
The CMAKE_CXX_STANDARD variable is used to initialize the CXX_STANDARD property.
From the property documentation:
For compilers that have no notion of a standard level, such as MSVC,
this has no effect.
For Visual Studio 2017 with plain CMake, the canonical way for C++17 (the default is C++14) is:
target_compile_options(optional2 PRIVATE /std:c++latest)

Using C++0x in Xcode 4.2 project via CMake

I'm using CMake to generate a project file for Xcode 4.2 on OSX Lion, and I'm using some of the C++0x features in LLVM like nullptr and auto. In order to use these, Xcode requires that 2 project settings be set:
C++ Language Dialect set to C++0x [-std=C++0x]
C++ Standard Library set to libc++ (LLVM C++ standard library with C++'0X support)
Currently every time I generate an Xcode project, I have to go in and manually adjust these settings.
Is there a way to specify these settings in CMake?
Thanks
after digging into this for a little, these are the commands to set the appropriate xcode settings:
set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x")
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -stdlib=libc++ -g -Wall")
I think setting the c++ flags is redundant, so it might also work without the last line.
hope that helps!
The first one you could change the CMAKE_CXX_FLAGS attribute and add it:
SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=C++0x")
As for selecting GCC instead of Clang you'll have to use something like:
Switching between GCC and Clang/LLVM using CMake
That will override the CLang default values to use GCC