使用 g++ 编译 C++11

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10363646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:57:09  来源:igfitidea点击:

Compiling C++11 with g++

c++c++11g++flags

提问by Rontogiannis Aristofanis

I'm trying to update my C++ compiler to C++11. I have searched a bit and I have come to the conclusion that I have to use the flag -std=c++0xor -std=gnu++0x, but I don't know many things about flags. Can anyone help me? (I'm using Ubuntu 12.04.)

我正在尝试将我的 C++ 编译器更新为 C++11。我搜索了一下,得出的结论是我必须使用标志-std=c++0x-std=gnu++0x,但我对标志了解不多。谁能帮我?(我使用的是 Ubuntu 12.04。)

Here is the error that I get from the compiler when I attempt to use a library which is included in C++11 (i.e. array):

这是我尝试使用包含在 C++11 中的库(即数组)时从编译器中得到的错误:

#include <array>
#include <iostream>

int main()
{
    std::array<int, 3> arr = {2, 3, 5};
    ...
}

This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.

此文件需要对即将推出的 ISO C++ 标准 C++0x 的编译器和库支持。此支持目前处于实验阶段,必须使用 -std=c++0x 或 -std=gnu++0x 编译器选项启用。

回答by Oskar N.

Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable.

标志(或编译器选项)只不过是传递给编译器可执行文件的普通命令行参数。

Assuming you are invoking g++ from the command line (terminal):

假设您从命令行(终端)调用 g++:

$ g++ -std=c++11 your_file.cpp -o your_program

$ g++ -std=c++11 your_file.cpp -o your_program

or

或者

$ g++ -std=c++0x your_file.cpp -o your_program

$ g++ -std=c++0x your_file.cpp -o your_program

if the above doesn't work.

如果上述方法不起作用。

回答by Harajyoti Das

You can check your g++by command:

您可以g++通过命令检查您的:

which g++
g++ --version

this will tell you which complier is currently it is pointing.

这将告诉您当前指向的是哪个编译器。

To switch to g++4.7 (assuming that you have installed it in your machine),run:

要切换到g++4.7(假设你已经在你的机器上安装了它),运行:

sudo update-alternatives --config gcc

There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-4.6   60        auto mode
  1            /usr/bin/gcc-4.6   60        manual mode
* 2            /usr/bin/gcc-4.7   40        manual mode

Then select 2as selection(My machine already pointing to g++4.7,so the *)

然后选择2作为选择(我的机器已经指向g++4.7,所以*)

Once you switch the complier then again run g++ --versionto check the switching has happened correctly.

切换编译器后,再次运行g++ --version以检查切换是否正确发生。

Now compile your program with

现在编译你的程序

g++ -std=c++11 your_file.cpp -o main

回答by Michael Slade

Your ubuntu definitely has a sufficiently recent version of g++. The flag to use is -std=c++0x.

你的 ubuntu 肯定有一个足够新的 g++ 版本。要使用的标志是-std=c++0x.

回答by Michael Slade

If you want to keep the GNU compiler extensions, use -std=gnu++0x rather than -std=c++0x. Here's a quote from the man page:

如果要保留 GNU 编译器扩展,请使用 -std=gnu++0x 而不是 -std=c++0x。这是手册页的引用:

The compiler can accept several base standards, such as c89 or c++98, and GNU dialects of those standards, such as gnu89 or gnu++98. By specifying a base standard, the compiler will accept all programs following that standard and those using GNU extensions that do not contradict it. For example, -std=c89 turns off certain features of GCC that are incompatible with ISO C90, such as the "asm" and "typeof" keywords, but not other GNU extensions that do not have a meaning in ISO C90, such as omitting the middle term of a "?:" expression. On the other hand, by specifying a GNU dialect of a standard, all features the compiler support are enabled, even when those features change the meaning of the base standard and some strict-conforming programs may be rejected. The particular standard is used by -pedantic to identify which features are GNU extensions given that version of the standard. For example-std=gnu89 -pedantic would warn about C++ style // comments, while -std=gnu99 -pedantic would not.

编译器可以接受多个基本标准,例如 c89 或 c++98,以及这些标准的 GNU 方言,例如 gnu89 或 gnu++98。通过指定基本标准,编译器将接受所有遵循该标准的程序以及那些使用与该标准不矛盾的 GNU 扩展的程序。例如,-std=c89 关闭 GCC 的某些与 ISO C90 不兼容的功能,例如“asm”和“typeof”关键字,但不会关闭在 ISO C90 中没有意义的其他 GNU 扩展,例如省略“?:”表达式的中间词。另一方面,通过指定标准的 GNU 方言,编译器支持的所有特性都被启用,即使这些特性改变了基本标准的含义并且一些严格遵守的程序可能会被拒绝。-pedantic 使用特定标准来确定哪些功能是给定该标准版本的 GNU 扩展。例如,-std=gnu89 -pedantic 会警告 C++ 风格的 // 注释,而 -std=gnu99 -pedantic 不会。

回答by yadhu

You can refer to following linkfor which features are supported in particular version of compiler. It has an exhaustive list of feature support in compiler. Looks GCC follows standard closely and implements before any other compiler.

您可以参考以下链接了解特定版本的编译器支持哪些功能。它有一个详尽的编译器功能支持列表。看起来 GCC 严格遵循标准并在任何其他编译器之前实现。

Regarding your question you can compile using

关于您的问题,您可以使用编译

  1. g++ -std=c++11for C++11
  2. g++ -std=c++14for C++14
  3. g++ -std=c++17for C++17
  4. g++ -std=c++2afor C++20, although all features of C++20 are not yet supported refer this linkfor feature support list in GCC.
  1. g++ -std=c++11对于 C++11
  2. g++ -std=c++14对于 C++14
  3. g++ -std=c++17对于 C++17
  4. g++ -std=c++2a对于 C++20,虽然尚不支持 C++20 的所有功能,请参阅此链接以获取 GCC 中的功能支持列表。

The list changes pretty fast, keep an eye on the list, if you are waiting for particular feature to be supported.

列表变化非常快,如果您正在等待支持特定功能,请密切关注列表。