在CMake中引入最低的C ++标准版本 [英] Inducing minimal C++ standard version in CMake

查看:66
本文介绍了在CMake中引入最低的C ++标准版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CMake具有不错的框架用于设置和定义C ++标准的显式值,通常:

CMake has a nice framework for setting and defining an explicit value for the C++ standard, typically:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)

但是,这显然不能完全满足我的需求,我宁愿声明至少需要 c ++ 11.我以为我可以代替:

However this does not clearly fit my needs, I'd rather states that I need at least c++11. I thought that I could just do instead:

$ cat CMakeLists.txt 
cmake_minimum_required(VERSION 3.7)
project(p CXX)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(foobar foobar.cxx)
target_compile_features(foobar PRIVATE cxx_nullptr)

其中

$ cat foobar.cxx 
int main()
{
  char * p = nullptr;
}

然而,在这种情况下,即使默认情况下, -std = c ++ 11 .html"rel =" nofollow noreferrer> g ++ 6.3.0 默认为 -std = c ++ 14 (技术上为 -std = gnu ++ 14 ):

However again in this case this forces me to use -std=c++11 eventhough by default g++ 6.3.0 default to -std=c++14 (technically -std=gnu++14):

$ c++ -dumpversion
6.3.0

导致:

$ make VERBOSE=1
[...]
make[2]: Entering directory '/tmp/p'
[ 50%] Building CXX object CMakeFiles/foobar.dir/foobar.cxx.o
/usr/bin/c++     -std=c++11 -o CMakeFiles/foobar.dir/foobar.cxx.o -c /tmp/p/foobar.cxx
[100%] Linking CXX executable foobar
/usr/bin/cmake -E cmake_link_script CMakeFiles/foobar.dir/link.txt --verbose=1

有没有办法说:在CMake中至少使用C ++ 11标准构建此项目"?

Is there a way to say: "Build this project with at least C++11 Standard" in CMake ?

对于使用g ++ 4.8.5构建的项目,通常会添加 -std = c ++ 11 ,但是对于使用g ++ 6.3.0构建的项目,它将保留默认(隐式) -std = c ++ 14

Typically for a project built using g++ 4.8.5 it would add -std=c++11 but for a project build with g++ 6.3.0 it would leave the default (implicit) -std=c++14

更新:以下内容超出了该问题的范围,但是由于我收到@ComicSansMS的冗长答复,因此我觉得需要澄清这一点.

Update: The following is out of the scope for the question, but since I received a lengthy answer from @ComicSansMS, I feel I need to clarify the need for this.

我正在戴Debian维护者的帽子,几个月前我被说服了,在项目中的cmake中设置显式的C ++标准版本是正确的方法,因此我的建议是

I am working with my Debian Maintainer hat on, and I was convinced a couple of months back that setting an explicit C++ standard version in cmake within a project was the right way to do, hence my proposal:

但是,这里有两件事混杂在一起:

However there are two things that get mixed here:

  1. 为要构建的库的界面定义c ++标准版本
  2. 为要构建的库的实现详细信息定义c ++标准版本.

从Debian维护者的角度来看,明确地设置C ++标准版本使得在更新库SONAME时很难重建一部分软件包文件.让我们考虑 GDCM 使用

From a Debian Maintainer perspective setting explicitly a C++ standard version makes it hard to rebuild a portion of the package archive when a library SONAME is being updated. Let's consider the case where GDCM is using the Poppler library. While the implementation details of GDCM are written using C++98, the fact that Poppler library has been build using the default (implicit) standard version of gcc-6 makes it suddenly a compilation failure for GDCM, since an explicit -std=c++98 is being passed.

因此,对于预期的实现,设置显式的c ++标准版本是有意义的(显然!),但对于接口的预期则不太清楚.绝大多数开源项目没有定义多个c ++ ABI(std :: string [98]和std :: string [11]),并假定将使用单个版本来运送二进制文件.在这种情况下,使用默认(隐式)gcc版本(至少当以官方Debian软件包上传时)构建c ++软件包非常重要.

So while for an implementation prospective, setting an explicit c++ standard version make sense (obviously!), it is a little less clear for an interface prospective. The vast majority of open-source projects do not define multiple c++ ABI (std::string[98] AND std::string[11]) and assume a single version will be used to ship the binary. In this case it makes it important for a c++ package to be build using the default (implicit) version of gcc (at least when uploaded as official Debian package).

推荐答案

您始终可以自己测试特定标准标志的存在.

You can always test for existence of the specific standard flags yourself.

首先检查 -std = c ++ 14 ,如果不存在,则检查 -std = c ++ 11 ,如果不存在不起作用然后出错.

First check for -std=c++14, and if it doesn't exist then check for -std=c++11, and if that doesn't work then error out.

可以轻松地使用 CheckCXXCompilerFlag 模块.

Flags can easily be checked with the CheckCXXCompilerFlag module.

这几天,您可能应该从 -std = c ++ 17 开始.您可能还需要添加对预发行标准版本的检查,例如 c ++ 1z (对于C ++ 17)和 c ++ 1y (对于C ++14).

These days you should probably start with -std=c++17 though. You might also want to add checks for the pre-release standard versions like c++1z (for C++17) and c++1y (for C++14).

从最高版本开始,然后逐步降低最低要求版本.在没有失败的情况下停止.

Start with the highest version, then work your way downward the minimum required version. Stop when it doesn't fail.

这篇关于在CMake中引入最低的C ++标准版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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