2

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.

jww
  • 97,681
  • 90
  • 411
  • 885
s_michael
  • 23
  • 1
  • 6
  • Visual Studio does not support C++17 in practice as of this writing. Also see [Support For C++11/14/17 Features (Modern C++)](https://msdn.microsoft.com/en-us/library/hh567368.aspx) on MSDN. They don't really support C++11 or C++14 well, either. We made some changes based on the support matrix advertised at MSDN, and the bug reports and crash reports rolled in. All the problems Microsoft's advertising crap caused made me sick to my stomach. – jww Jul 29 '17 at 20:24
  • 1
    Possible duplicate of [How to enable /std:c++17 in VS2017 with CMake](https://stackoverflow.com/questions/44960715/how-to-enable-stdc17-in-vs2017-with-cmake) – Florian Jul 31 '17 at 16:07

1 Answers1

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)
utopia
  • 1,477
  • 8
  • 7
  • 1
    thank you; that works provided I put the statement after the 'add_executable(optional2 ${SOURCE_FILES})' statement. – s_michael Jul 29 '17 at 19:41
  • @SethCurry that's expected. the target `optional2` does not exist until the call to `add_executable` creates it – Richard Hodges Jul 29 '17 at 19:44