如何使CMake通过基于GCC版本的std = c ++ 14/c ++ 1y或c ++ 17/c ++ 1z? [英] How to get CMake to pass either std=c++14/c++1y or c++17/c++1z based on GCC version?

查看:2416
本文介绍了如何使CMake通过基于GCC版本的std = c ++ 14/c ++ 1y或c ++ 17/c ++ 1z?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC 4.x不接受C ++ 14代码的--std=c++14开关-取而代之的是--std=c++1y.更高的版本采用--std=c++1z,但(可能)不是--std=c++17,尚未设置(在2016年编写).也许C ++ 11也有类似的问题.

GCC 4.x doesn't accept the --std=c++14 switch for C++14 code - it takes --std=c++1y instead. Later versions take --std=c++1z but (probably) not --std=c++17 which has not been set yet (writing this in 2016). Perhaps there are similar issues with C++11.

CMake是否具有某种功能(也许作为模块)可以根据GCC版本传递正确的开关?

Does CMake have some facility (perhaps as a module) to pass the correct switch according to the GCC version?

推荐答案

当要指定特定的C ++版本时,建议在CMake 3.1及更高版本中使用的方法是使用CXX_STANDARDCXX_STANDARD_REQUIREDCXX_EXTENSIONS目标属性或其等效变量以指定目标默认值.可以在此处中找到完整的详细信息,但简称为像这样的东西:

When wanting to specify a particular C++ version, the recommended way to do this with CMake 3.1 and later is to use the CXX_STANDARD, CXX_STANDARD_REQUIRED and CXX_EXTENSIONS target properties, or their variable equivalents to specify target defaults. Full details can be found here, but the short version goes something like this:

cmake_minimum_required(VERSION 3.1)
project(Example)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# ... Define targets, etc. as usual

然后,CMake应根据编译器支持的内容为所请求的C ++标准选择适当的编译器标志,否则,如果不支持所请求的标准,则出错.

CMake should then select the appropriate compiler flag for the requested C++ standard based on what the compiler supports, or error out if it doesn't support the requested standard.

还应注意,CMake可以将目标升级为使用比其CXX_STANDARD target属性指定的语言标准更高的语言标准.使用编译功能要求(如@FlorianWolters答案中所述)可以提高语言标准要求.实际上,CMake将始终选择CXX_STANDARD目标属性或目标上设置的编译功能要求所指定的更强语言要求.另请注意,从3.10.1版开始的CMake文档并未准确反映CXX_EXTENSIONS与编译功能的交互方式,因为CXX_EXTENSIONS仅在大多数通用编译器也指定了CXX_STANDARD的情况下才生效(因为它们与一个编译器标志).

It should also be noted that CMake may upgrade the target to use a later language standard than the one specified by its CXX_STANDARD target property. The use of compile feature requirements (as mentioned in @FlorianWolters answer) can raise the language standard requirement. In fact, CMake will always pick the stronger language requirement specified by either the CXX_STANDARD target property or the compile feature requirements set on the target. Note also that the CMake documentation as of 3.10.1 does not accurately reflect the way CXX_EXTENSIONS interacts with compile features, as CXX_EXTENSIONS only takes effect if CXX_STANDARD is also specified for most common compilers (since they are specified together with the one compiler flag).

这篇关于如何使CMake通过基于GCC版本的std = c ++ 14/c ++ 1y或c ++ 17/c ++ 1z?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆