Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable C++17 in CMake

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. 
like image 798
MiP Avatar asked Aug 15 '17 07:08

MiP


People also ask

Does CMake work with C?

In the C/C++ ecosystem, the best tool for project configuration is CMake. CMake allows you to specify the build of a project, in files named CMakeLists. txt, with a simple syntax (much simpler than writing Makefiles).

What is Cmakelist?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.

What is option in CMake?

Provide a boolean option that the user can optionally select. option(<variable> "<help_text>" [value]) If no initial <value> is provided, boolean OFF is the default value. If <variable> is already set as a normal or cache variable, then the command does nothing (see policy CMP0077 ).

What are targets in CMake?

Introduction. A CMake-based buildsystem is organized as a set of high-level logical targets. Each target corresponds to an executable or library, or is a custom target containing custom commands.

How to enable C++11 and later in CMake?

Enabling C++11 And Later In CMake 1 Setting the C++ standard directly. Valid values for CMAKE_CXX_STANDARD are 98, 11 and 14, with 17 also being added in CMake 3.8 and 20 added in CMake 3.12. 2 Setting the C++ standard based on features. ... 3 Optional features. ... 4 Concluding remarks. ...

How do I set the C++ standard in CMake?

Setting the C++ standard directly. The simplest way to use a particular C++ standard in your project is to add the following two variable definitions before you define any targets: Valid values for CMAKE_CXX_STANDARD are 98, 11 and 14, with 17 also being added in CMake 3.8.

How do I enable CMake in Visual Studio 2017?

Select Configuration Properties, C/C++, Language. In C++ Language Standard, choose the language standard to support from the dropdown control, then choose OK or Apply to save your changes. Visual Studio 2017 (15.7+) supports CMake projects.

How do I use the CMake_CXX_standard variable?

CMake 3.1 introduced the CMAKE_CXX_STANDARDvariable that you can use. If you know that you will always have CMake 3.1 or later available, you can just write this in your top-level CMakeLists.txt file, or put it right before any new target is defined: set (CMAKE_CXX_STANDARD 11)


1 Answers

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)

like image 122
nbout Avatar answered Oct 01 '22 12:10

nbout