【问题标题】:setting g++ mode to C++11将 g++ 模式设置为 C++11
【发布时间】:2021-01-06 08:35:59
【问题描述】:

我正在尝试构建需要 C++11 的 cmake 源代码。 构建停止,显然抱怨是未检测到 C++11。 g++模式其实设置为-std=gnu++17

这是控制台日志的一部分

---------------------------------------------
CMake 3.18.20200919, Copyright 2000-2020 Kitware, Inc. and Contributors
Found GNU toolchain
C compiler on this system is: gcc   
C++ compiler on this system is: g++  -std=gnu++17  
Makefile processor on this system is: make
g++ has setenv
g++ has unsetenv
g++ does not have environ in stdlib.h
g++ has stl wstring
g++ has <ext/stdio_filebuf.h>
---------------------------------------------
g++  -std=gnu++17      -DCMAKE_BOOTSTRAP    -DCMake_HAVE_CXX_MAKE_UNIQUE=1  -c $HOME/Apps/CMake-master/Source/cmAddCustomCommandCommand.cxx -o cmAddCustomCommandCommand.o

这是日志文件中错误的一部分...

In file included from /usr/include/c++/5/unordered_map:35:0,
                 from cmake_bootstrap_11920_test.cxx:4:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \
  ^
cmake_bootstrap_11920_test.cxx:7:2: error: #error "Compiler is not in a mode aware of C++11."
 #error "Compiler is not in a mode aware of C++11."
  ^
cmake_bootstrap_11920_test.cxx:70:16: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
   int Member = 1;

在网上四处寻找,我注意到 C++11 仅在 gcc 版本 4.6 之后可用。 我检查了我的版本,似乎在上面。

g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609

我了解 -std=c++11 标志用于启用 g++ 中的 C++11 功能,但我似乎不知道在这种情况下我在做什么。 我尝试编辑 CompileFlags.cmake 文件,但没有发生任何变化。

我遇到了this page,它指向我正在使用的 cmake 源。 它说...

bootstrap: Require compiler mode aware of C++11

Some compilers have enough features enabled in their default modes to
pass our simple C++11 unique_ptr check but do not enable enough to build
CMake.  Poison this case so that we choose one of the explicit `-std=`
options for such compilers.

不知道这到底是什么意思。

如何将 g++ 模式更改为 C++11,以便在运行引导命令时使用 C++11? 或者换句话说,如何将std改为指向C++11(-std=c++11)?

【问题讨论】:

  • 欢迎来到 Stack Overflow!您对问题的描述非常...模糊,无法为您提供帮助。请相应地咨询How to Askedit 您的问题(至少包括您收到的确切错误消息)。
  • 如果你是 Ubuntu,使用 apt 安装 CMake 然后从源代码构建它更容易。仅当您首先没有 CMake 时才需要引导。此外,如果引导程序不起作用,那么向套件报告错误报告可能是一个好主意。
  • 感谢您的提示。我什至没有考虑使用 cmake,只是按照页面上的说明进行操作。我想我会尝试使用cmake。 @dboy 你能不能给你的帖子一些细节,因为集合 (CMAKE_CXX_STANDARD 11) 已经设置为可以使用,但前提是不满足一些条件。我不知道我是否应该删除这些条件。
  • @fdk cmake 轻而易举。这就是我喜欢看到的。没有错误的构建。

标签: c++11 cmake g++


【解决方案1】:

首先,您的主机 PC 中安装了 g++ 版本 5.4.0,这很好,因为这意味着它也支持您想要使用的 C++11。 要设置它,您可以在 CMakeList.txt 文件中定义它:

set (CMAKE_CXX_STANDARD 11)

这应该可以解决问题。 另请查看文档:

https://cmake.org/cmake/help/v3.1/variable/CMAKE_CXX_STANDARD.html

通常,我建议使用您的编译器支持的最新标准 (https://gcc.gnu.org/projects/cxx-status.html),因为您还将获得该标准中引入的最新功能。如果您使用的是遗留代码,则例外。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2012-05-08
    • 2012-12-02
    • 2016-04-25
    相关资源
    最近更新 更多