用 g++ 编译 c++14 代码
Compile c++14-code with g++
我在 Ubuntu 14.04 LTS 上使用 g++ 4.8.4。尝试使用“-std=c++14”进行编译时,出现此错误:
g++: error unrecognized command line option '-std=c++14'
使用“-std=c++11”编译工作正常,所以我不确定发生了什么。 g++ 真的不支持 c++14 吗?我使用了错误的命令行选项吗?
我使用了 "sudo apt-get install g++",它应该会自动检索最新版本,对吗?
GCC 4.8 不支持 -std=c++14
标志。如果你想使用 C++14 特性,你需要使用 -std=c++1y
进行编译。使用 godbolt.org 看来支持 -std=c++14
的最早版本是 GCC 4.9.0 或 Clang 3.5.0
G++ 通过 -std=c++14
和 -std=c++1y
支持 C++14。后者是该标准的通用名称,之后才知道它将在哪一年发布。在旧版本(包括您的版本)中,仅接受后者,因为这些版本发布时的发布年份尚不清楚。
I used "sudo apt-get install g++" which should automatically retrieve the latest version, is that correct?
它安装 Ubuntu 存储库中可用的最新版本,而不是现有的最新版本。
最新的 GCC 版本是 5.2。
对于gcc 4.8.4
你需要在以后的版本中使用-std=c++1y
,看起来从5.2
开始你可以使用-std=c++14
。
如果我们查看 gcc online documents we can find the manuals for each version of gcc and we can see by going to Dialect options for 4.9.3 under the GCC 4.9.3 manual 它说:
‘c++1y’
The next revision of the ISO C++ standard, tentatively planned for 2014. Support is highly experimental, and will almost certainly change in incompatible ways in future releases.
所以直到 4.9.3
你必须使用 -std=c++1y
而 gcc 5.2 options 说:
‘c++14’
‘c++1y’
The 2014 ISO C++ standard plus amendments. The name ‘c++1y’ is deprecated.
我不清楚为什么将其列在 Options Controlling C Dialect
下,但这就是文档当前的组织方式。
按照 https://gist.github.com/application2000/73fd6f4bf1be6600a2cf9f56315a2d91 中的说明在 Ubuntu 14.04 上设置您需要的 gcc 版本 - gcc 5 或 gcc 6。说明包括配置 update-alternatives
以允许您根据需要在版本之间切换。
我在 Ubuntu 14.04 LTS 上使用 g++ 4.8.4。尝试使用“-std=c++14”进行编译时,出现此错误:
g++: error unrecognized command line option '-std=c++14'
使用“-std=c++11”编译工作正常,所以我不确定发生了什么。 g++ 真的不支持 c++14 吗?我使用了错误的命令行选项吗?
我使用了 "sudo apt-get install g++",它应该会自动检索最新版本,对吗?
GCC 4.8 不支持 -std=c++14
标志。如果你想使用 C++14 特性,你需要使用 -std=c++1y
进行编译。使用 godbolt.org 看来支持 -std=c++14
的最早版本是 GCC 4.9.0 或 Clang 3.5.0
G++ 通过 -std=c++14
和 -std=c++1y
支持 C++14。后者是该标准的通用名称,之后才知道它将在哪一年发布。在旧版本(包括您的版本)中,仅接受后者,因为这些版本发布时的发布年份尚不清楚。
I used "sudo apt-get install g++" which should automatically retrieve the latest version, is that correct?
它安装 Ubuntu 存储库中可用的最新版本,而不是现有的最新版本。
最新的 GCC 版本是 5.2。
对于gcc 4.8.4
你需要在以后的版本中使用-std=c++1y
,看起来从5.2
开始你可以使用-std=c++14
。
如果我们查看 gcc online documents we can find the manuals for each version of gcc and we can see by going to Dialect options for 4.9.3 under the GCC 4.9.3 manual 它说:
‘c++1y’
The next revision of the ISO C++ standard, tentatively planned for 2014. Support is highly experimental, and will almost certainly change in incompatible ways in future releases.
所以直到 4.9.3
你必须使用 -std=c++1y
而 gcc 5.2 options 说:
‘c++14’ ‘c++1y’
The 2014 ISO C++ standard plus amendments. The name ‘c++1y’ is deprecated.
我不清楚为什么将其列在 Options Controlling C Dialect
下,但这就是文档当前的组织方式。
按照 https://gist.github.com/application2000/73fd6f4bf1be6600a2cf9f56315a2d91 中的说明在 Ubuntu 14.04 上设置您需要的 gcc 版本 - gcc 5 或 gcc 6。说明包括配置 update-alternatives
以允许您根据需要在版本之间切换。