简体   繁体   English

编译器标准支持(c++11、c++14、c++17)

[英]Compiler standards support (c++11, c++14, c++17)

How do I find which standards my GCC compiler supports?如何找到我的 GCC 编译器支持哪些标准? I don't mean how do I find out at the compilation time what C++ standard is being used (checking defined constants), but before compiling, how can I check available standards to use (ie for flag -std=c++?? )?我不是说我如何在编译时找出正在使用的 C++ 标准(检查定义的常量),而是在编译之前,我如何检查可用的标准(即标志-std=c++?? )?

The information is not present in man g++ .该信息不存在于man g++中。

I can check out my GCC version by g++ --version besides manually trying the options?除了手动尝试选项之外,我还可以通过g++ --version查看我的 GCC 版本吗?

Is it possible to find somewhere table of GCC versions and supported standards?是否可以在某处找到 GCC 版本和支持标准的表格?

So, after a bit of a struggle trying to dust off my sed skills, I was able to come up with this command:因此,在努力学习我的sed技能之后,我能够想出这个命令:

gcc -v --help 2> /dev/null | sed -n '/^ *-std=\([^<][^ ]\+\).*/ {s//\1/p}'

It processes the output of g++ -v --help (silencing the extra info it prints to stderr ), matches lines that start with -std= then captures the values.它处理g++ -v --help的输出(将它打印到stderr的额外信息静音),匹配以-std=开头的行,然后捕获值。 The ^< is to block the -std=<standard> line of the help. ^<用于阻止帮助的-std=<standard>行。 Here is some example output for GCC 9:以下是 GCC 9 的一些示例输出:

f2003
f2008
f2008ts
f2018
f95
gnu
legacy
c++03
c++0x
c++11
c++14
c++17
c++1y
c++1z
c++2a
c++98
c11
c17
c18
c1x
c2x
c89
c90
c99
c9x
gnu++03
gnu++0x
gnu++11
gnu++14
gnu++17
gnu++1y
gnu++1z
gnu++2a
gnu++98
gnu11
gnu17
gnu18
gnu1x
gnu2x
gnu89
gnu90
gnu99
gnu9x
iso9899:1990
iso9899:199409
iso9899:1999
iso9899:199x
iso9899:2011
iso9899:2017
iso9899:2018

You can add a grep in the middle to filter based on help description text, which is conveniently consistent in the help output.可以在中间加一个grep ,根据帮助描述文本进行过滤,方便在帮助输出中保持一致。 Eg if you want to drop the deprecated ones:例如,如果你想删除已弃用的那些:

gcc -v --help 2> /dev/null | grep -iv deprecated | sed -n '/^ *-std=\([^<][^ ]\+\).*/ {s//\1/p}'

If you want to list just non-deprecated C++:如果你只想列出未弃用的 C++:

gcc -v --help 2> /dev/null | grep -iv deprecated | grep "C++" | sed -n '/^ *-std=\([^<][^ ]\+\).*/ {s//\1/p}'

If you want to list just non-deprecated C:如果你只想列出未弃用的 C:

gcc -v --help 2> /dev/null | grep -iv deprecated | grep "C " | sed -n '/^ *-std=\([^<][^ ]\+\).*/ {s//\1/p}'

Those are pretty hacky and rely on "deprecated", "C++" and/or "C" (note the space at the end of grep "C " .) appearing in the help description for each standard name but they seem to work.这些非常老套,并且依赖于出现在每个标准名称的帮助描述中的“已弃用”、“C++”和/或“C”(注意grep "C "末尾的空格),但它们似乎有效。

You could similarly filter out eg "same as" to get rid of synonymous ones, etc.您可以类似地过滤掉例如“相同”以摆脱同义词等。

This information is available on GCC official website.这些信息可以在 GCC 官方网站上找到。 Here are the relevant tables:以下是相关表格:

C++11 features support C++11 特性支持

C++14 features support C++14 特性支持

C++17 features support C++17 特性支持

C++20 features support C++20 特性支持

The comment that @oldMammuth made is almost correct, gcc and g++ do actually have a way to print the language standards they supports. @oldMammuth 的评论几乎是正确的, gccg++实际上有办法打印它们支持的语言标准。 It's just not well documented.它只是没有很好的记录。 I'd say it was basically hidden if it wasn't listed in parentheses below the help text of the --help argument.如果它没有列在--help参数的帮助文本下方的括号中,我会说它基本上是隐藏的。 The way it's done is by going through the GNU compiler toolchain and asking the particular compiler instance you're using for it's --help text.它的完成方式是通过 GNU 编译器工具链并询问您正在使用的特定编译器实例的--help文本。 I actually just learned this after having to do this research for my own project, but in order to bundle a whole bunch of compilers under one program, gcc and g++ do exactly that, they use the main executable to serve as a middleman for communicating with the compiler, assembler, and linker processes.实际上,我是在为自己的项目进行这项研究之后才了解到这一点的,但是为了将一大堆编译器捆绑在一个程序下, gccg++正是这样做的,它们使用主可执行文件作为中间人与编译器、汇编器和链接器进程。 In order to access the help text to get the supported language standards of a given compiler version, you have to ask the compiler, not gcc or g++ .为了访问帮助文本以获得给定编译器版本支持的语言标准,您必须询问编译器,而不是gccg++

Since at this point, I'm getting tired of typing both commands, the rest of this will assume we're using gcc ;因为在这一点上,我已经厌倦了输入这两个命令,剩下的将假设我们正在使用gcc despite the fact that both commands are virtually interchangeable and basically the same middleman with a different name.尽管这两个命令实际上是可以互换的,而且基本上是同一个中间人,但名称不同。

You can get the path to said compiler by using gcc -print-prog-name=cc1 .您可以使用gcc -print-prog-name=cc1获取所述编译器的路径。 On my system this is /usr/lib/gcc/x86_64-linux-gnu/8/cc1 .在我的系统上,这是/usr/lib/gcc/x86_64-linux-gnu/8/cc1 Then, just call said executable with the --help parameter, and you're all set!然后,只需使用--help参数调用所述可执行文件,一切就绪! Beware , there are hundreds of help parameter entries.当心有数百个帮助参数条目。 I actually recommend piping the output through grep and using a regex to find them because otherwise there's so much extra information it's really annoying.我实际上建议通过grep管道输出并使用正则表达式来查找它们,否则会有太多额外信息,这真的很烦人。

ALTERNATIVELY:或者:

You can use gcc -v --help as stated in the gcc help text, to print the help dialogues for each program in the given tool-chain.您可以按照gcc帮助文本中的说明使用gcc -v --help来打印给定工具链中每个程序的帮助对话框。 This does result in a lot more output however.然而,这确实会产生更多的输出。

Again, my recommendation is to use a regex to search through the output and find the standard versions supported.同样,我的建议是使用正则表达式搜索输出并找到支持的标准版本。 gcc also supports more languages than C and C++, including but not limited to, Fortran and Go. gcc还支持比 C 和 C++ 更多的语言,包括但不限于 Fortran 和 Go。

gcc and g++ do not have a command line option to check this out. gcc 和 g++ 没有命令行选项来检查这一点。 It would be nice that the -v option would tell something about the supported standards.如果 -v 选项能够说明支持的标准,那就太好了。 Instead you can check the online docs at gcc Standards and the useful synopsis at cppreference.com .相反,您可以查看gcc Standards上的在线文档和cppreference.com上有用的概要。

According to cppreference , full support of c++11 came with gcc 4.8.1;根据cppreference ,gcc 4.8.1 完全支持 c++11;

To have full support of c++14 (with some of the new features of c++17), instead, you need gcc 5.0 and above.要完全支持 c++14(具有 c++17 的一些新功能),您需要 gcc 5.0 及更高版本。

Here's a table I found on the inte.net这是我在 inte.net 上找到的表格

GCC Version   C++ Standard
4.9             C++11
5               C++14
6               C++14
7               C++17
8               C++17
9               C++20
10              C++20
11              C++20
12              C++20
13              C++20

Note that these are the minimum standards that the GCC versions support.请注意,这些是 GCC 版本支持的最低标准。 Some versions may also support newer standards.某些版本可能还支持更新的标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM