如何在CMake中启用C ++ 17 [英] How to enable C++17 in CMake
问题描述
我正在使用VS 15.3,它支持集成的CMake 3.8.在不为每个特定的编译器编写标志的情况下,如何定位C ++ 17?我当前的全局设置无效:
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")
我希望CMake在生成VS解决方案文件时添加"/std:c ++ lastest"或等效名称,但未找到c ++ 17标志,导致编译器错误:
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.
推荐答案
您的方法是正确的,但不适用于3.10之前版本的CMake上的MSVC.
Your approach is the correct one, but it will not work for MSVC on versions of CMake prior to 3.10.
从 CMake 3.9 文档:
对于没有标准级别概念的编译器(例如MSVC),这无效.
For compilers that have no notion of a standard level, such as MSVC, this has no effect.
简而言之,尚未对CMake进行更新以适应添加到VC ++ 2017的标准标志.
In short, CMake haven't been updated to accommodate for the standard flags added to VC++ 2017.
您必须检测是否使用了VC ++ 2017(或更高版本),现在自己添加相应的标志.
You have to detect if VC++ 2017 (or later) is used and add the corresponding flags yourself for now.
在CMake 3.10(及更高版本)中,此问题已在VC ++的较新版本中得到修复.请参见 3.10文档.
In CMake 3.10 (and later) this have been fixed for newer version of VC++. See the 3.10 documentation.
这篇关于如何在CMake中启用C ++ 17的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!