使用 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
Which C++ standard is the default when compiling with g++?
提问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 cmd
the 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 string
to const char*
.
在 Windows 上,如果我输入cmd
命令g++ example.cpp
,它将失败。这是错误的,我认为主要是一长串由于连接器抱怨不能够从转换string
到const 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检查
$ g++ example.cpp -g
Compile program with -g flag to generate debug info$ gdb a.out
Debug program with gdb(gdb) b main
Put a breakpoint at main(gdb) run
Run program (will pause at breakpoint)(gdb) info source
$ g++ example.cpp -g
使用 -g 标志编译程序以生成调试信息$ gdb a.out
使用 gdb 调试程序(gdb) b main
在 main 处放置一个断点(gdb) run
运行程序(将在断点处暂停)(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++ --version
in 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
__cplusplus
which 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::string
for 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::ifstream
constructor that accepts a const std::string &
. Before C++11, a std::string
could 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++ -v
will 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++ 的默认版本。