使用 g++ 编译时,哪个 C++ 标准是默认的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44734397/
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-28 15:38:02  来源:igfitidea点击:

Which C++ standard is the default when compiling with g++?

c++g++mingw

提问by Manuel

I have a piece of code that looks like the following. Let's say it's in a file named example.cpp

我有一段如下所示的代码。假设它在一个名为的文件中example.cpp

#include <fstream>
#include <string> // line added after edit for clarity

int main() {
    std::string filename = "input.txt";
    std::ifstream in(filename);

    return 0;
}

On a windows, if I type in the cmdthe command g++ example.cpp, it will fail. It's a long list of errors I think mostly due to the linker complaining about not being able to convert from stringto const char*.

在 Windows 上,如果我输入cmd命令g++ example.cpp,它将失败。这是错误的,我认为主要是一长串由于连接器抱怨不能够从转换stringconst char*

But if I run the compiler using an additional argument like so: g++ -std=c++17 example.cpp, it will compile and work fine with no problems.

但是,如果我使用像这样的附加参数运行编译器:g++ -std=c++17 example.cpp,它将编译并正常工作,没有任何问题。

What happens when I run the former command? I'm guessing a default versionstandard of the C++ compiler gets called, but I don't know which? And as a programmer/developer, should I always use the latter command with the extra argument?

当我运行前一个命令时会发生什么?我猜会调用 C++ 编译器的默认版本标准,但我不知道是哪个?作为程序员/开发人员,我应该总是使用带有额外参数的后一个命令吗?

回答by Michael Burr

If your version of g++is later than 4.7 I think you can find the default version of C++ standard supported like so:

如果您的版本g++高于 4.7,我想您可以找到支持的 C++ 标准的默认版本,如下所示:

g++ -dM -E -x c++  /dev/null | grep -F __cplusplus

An example from my machine:

我机器上的一个例子:

mburr@mint17 ~ $ g++ --version | head -1
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
mburr@mint17 ~ $ g++ -dM -E -x c++  /dev/null | grep -F __cplusplus
#define __cplusplus 199711L

Some references:

一些参考:

回答by Basile Starynkevitch

I'm guessing a default version of the C++ compiler gets called, but I don't know which?

我猜会调用 C++ 编译器的默认版本,但我不知道是哪个版本?

This is only guessable by reading the documentation of your particular compiler version.

这只能通过阅读您的特定编译器版本的文档来猜测。

If using a recentGCC, I recommend first to understand what version are you using by running

如果使用最近的GCC,我建议首先通过运行来了解您使用的是什么版本

g++ -v

or

或者

g++ --version

and then refer to the version of the particular release of GCC. For example for GCC 7, read GCC 7 changesetc

然后参考 GCC 特定版本的版本。例如对于 GCC 7,阅读GCC 7 更改

Alternatively, run

或者,运行

g++ -dumpspecs

and decipher the default so called spec file.

并破译默认的所谓规范文件

BTW, you could ensure (e.g. in some of your common header file) that C++ is at least C++17 by coding

顺便说一句,您可以通过编码确保(例如在您的一些常见头文件中)C++ 至少是 C++17

 #if __cplusplus < 201412L
 #error expecting C++17 standard
 #endif

and I actually recommend doing it that way.

我实际上建议这样做。

PS. Actually, think of C++98 & C++17 being two differentlanguages (e.g. like Ocaml4 and C++11 are). Require your user to have a compiler supporting some defined language standard (e.g. C++11), not some particular version of GCC. Read also about package managers.

附注。实际上,将 C++98 和 C++17 视为两种不同的语言(例如 Ocaml4 和 C++11 是)。要求您的用户拥有支持某些已定义语言标准(例如 C++11)的编译器,而不是某些特定版本的GCC。另请阅读有关包管理器的信息

回答by AnInquiringMind

I believe that it is possible to tell by looking at the man page (at least for g++):

我相信可以通过查看手册页来判断(至少对于 g++):

Under the description of -std, the man page lists all C++ standards, including the GNU dialects. Under one specific standard, it is rather inconspicuously stated, This is the default for C++ code.(there is an analogous statement for C standards: This is the default for C code.).

在 的描述下-std,手册页列出了所有 C++ 标准,包括 GNU 方言。在一个特定的标准下,它是相当不显眼的陈述,This is the default for C++ code.(C 标准有一个类似的声明:)This is the default for C code.

For instance, for g++/gcc version 5.4.0, this is listed under gnu++98/gnu++03, whereas for g++/gcc version 6.4.0, this is listed under gnu++14.

例如,对于g++/gcc version 5.4.0,这是在 下列出的gnu++98/gnu++03,而对于g++/gcc version 6.4.0,这是在 下列出的gnu++14

回答by Burrito

You can also check with gdb

你也可以用gdb检查

  1. $ g++ example.cpp -gCompile program with -g flag to generate debug info
  2. $ gdb a.outDebug program with gdb
  3. (gdb) b mainPut a breakpoint at main
  4. (gdb) runRun program (will pause at breakpoint)
  5. (gdb) info source
  1. $ g++ example.cpp -g使用 -g 标志编译程序以生成调试信息
  2. $ gdb a.out使用 gdb 调试程序
  3. (gdb) b main在 main 处放置一个断点
  4. (gdb) run运行程序(将在断点处暂停)
  5. (gdb) info source

Prints out something like:

打印出类似的东西:

Current source file is example.cpp
Compilation directory is /home/xxx/cpp
Located in /home/xxx/cpp/example.cpp
Contains 7 lines.
Source language is c++.
Producer is GNU C++14 6.3.0 20170516 -mtune=generic -march=x86-64 -g.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.

There is the standard used by compiler: Producer is GNU C++14

编译器使用的标准是: Producer is GNU C++14

If you recompile your program using -std=c++11(for example), gdb detects it: Producer is GNU C++11

如果您使用-std=c++11(例如)重新编译程序,gdb 会检测到它: Producer is GNU C++11

回答by Ding-Yi Chen

g++ man page actually tells what is the default standard for C++ code.

g++ 手册页实际上说明了 C++ 代码的默认标准是什么。

Use following script to show the relevant part:

使用以下脚本显示相关部分:

man g++ | col -b | grep -B 1 -e '-std.* default'

For example, in RHEL 6 g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), the output:

例如,在 RHEL 6 g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23) 中,输出:

         gnu++98
           GNU dialect of -std=c++98.  This is the default for C++ code.

And in Fedora 28 g++ (GCC) 8.1.1 20180502 (Red Hat 8.1.1-1), the output:

在 Fedora 28 g++ (GCC) 8.1.1 20180502 (Red Hat 8.1.1-1) 中,输出:

       gnu++1y
           GNU dialect of -std=c++14.  This is the default for C++ code.  The name gnu++1y is deprecated.

回答by Bathsheba

Typing g++ --versionin your command shell will reveal the version of the compiler, and from that you can infer the default standard. So you can't tell directly but you can inferit, with some effort.

g++ --version在您的命令 shell 中键入将显示编译器的版本,您可以从中推断出默认标准。所以你不能直接告诉,但你可以通过一些努力来推断它。

Compilers are supposedto #define__cpluspluswhich can be used to extract the standard that they purport to implement at compile time; but many don't do this yet.

编译器应该#define__cplusplus可以使用的,它们声称在编译时执行提取标准; 但很多人还没有这样做。

(And don't forget to include all the C++ standard library headers you need: where is the one for std::stringfor example? Don't rely on your C++ standard library implementation including other headers automatically - in doing that you are not writing portable C++.)

(并且不要忘记包含您需要的所有 C++ 标准库头文件:std::string例如,在哪里?不要依赖您的 C++ 标准库实现自动包括其他头文件 - 这样做您不是在编写可移植的 C++。 )

回答by Peter

Your question is specific to gnu compilers, so probably better to tag it appropriately, rather than just C++ and C++11.

您的问题特定于 gnu 编译器,因此可能更好地对其进行适当标记,而不仅仅是 C++ 和 C++11。

Your code will compile with any compilers (and associated libraries) compliant with C++11 and later.

您的代码将使用符合 C++11 及更高版本的任何编译器(和相关库)进行编译。

The reason is that C++11 introduced a std::ifstreamconstructor that accepts a const std::string &. Before C++11, a std::stringcould not be passed, and it would be necessary in your code to pass filename.c_str()rather than filename.

原因是 C++11 引入了一个std::ifstream接受const std::string &. 在 C++11 之前,std::string无法传递 a,并且在您的代码中必须传递filename.c_str()而不是filename.

According to information from gnu, https://gcc.gnu.org/projects/cxx-status.html#cxx11, gcc.4.8.1 was the first version to fully support C++11. At the command line g++ -vwill prod g++to telling you its version number.

根据来自 gnu 的信息,https://gcc.gnu.org/projects/cxx-status.html#cxx11,gcc.4.8.1 是第一个完全支持 C++11 的版本。在命令行中g++ -v会提示g++你它的版本号。

If you dig into documentation, you might be able to find the version/subversion that first supported enough features so your code - as given - would compile. But such a version would support some C++11 features and not others.

如果您深入研究文档,您可能会找到首先支持足够功能的版本/颠覆版本,以便您的代码(如给定的)可以编译。但是这样的版本将支持某些 C++11 功能而不是其他功能。

Since windows isn't distributed with g++, you will have whatever version someone (you?) has chosen to install. There will be no default version of g++ associated with your version of windows.

由于 Windows 不随 g++ 一起分发,因此您将拥有某人(您?)选择安装的任何版本。不会有与您的 Windows 版本相关联的 g++ 的默认版本。