用 g++ 编译 c++14 代码

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

Compile c++14-code with g++

c++g++ubuntu-14.04c++14

提问by Silverlan

I'm using g++ 4.8.4 on Ubuntu 14.04 LTS. When trying to compile with '-std=c++14', I get this error:

我在 Ubuntu 14.04 LTS 上使用 g++ 4.8.4。尝试使用“-std=c++14”进行编译时,出现此错误:

g++: error unrecognized command line option '-std=c++14'

Compiling with '-std=c++11' works fine, so I'm not sure what's going on. Does g++ really have no support for c++14 yet? Am I using a wrong command line option?

使用 '-std=c++11' 编译工作正常,所以我不确定发生了什么。g++ 真的不支持 c++14 吗?我是否使用了错误的命令行选项?

I used "sudo apt-get install g++" which should automatically retrieve the latest version, is that correct?

我使用了“sudo apt-get install g++”,它应该会自动检索最新版本,对吗?

回答by Shafik Yaghmour

For gcc 4.8.4you need to use -std=c++1yin later versions, looks like starting with 5.2you can use -std=c++14.

对于gcc 4.8.4你需要-std=c++1y在以后的版本中使用,看起来5.2你可以使用-std=c++14.

If we look at the gcc online documentswe can find the manuals for each version of gcc and we can see by going to Dialect options for 4.9.3under the GCC 4.9.3 manualit says:

如果我们查看 gcc在线文档,我们可以找到每个版本的 gcc 的手册,我们可以在GCC 4.9.3 手册下的4.9.3 的方言选项中看到它说:

‘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.

'c++1y'

ISO C++ 标准的下一个修订版,暂定于 2014 年进行。支持是高度实验性的,几乎肯定会在未来版本中以不兼容的方式进行更改。

So up till 4.9.3you had to use -std=c++1ywhile the gcc 5.2 optionssay:

因此,截至4.9.3您必须使用-std=c++1yGCC 5.2选项说:

‘c++14' ‘c++1y'

The 2014 ISO C++ standard plus amendments. The name ‘c++1y' is deprecated.

'c++14' 'c++1y'

2014 ISO C++ 标准加上修正。名称“c++1y”已弃用。

It is not clear to me why this is listed under Options Controlling C Dialectbut that is how the documents are currently organized.

我不清楚为什么将其列在下面,Options Controlling C Dialect但这就是目前文件的组织方式。

回答by NathanOliver

The -std=c++14flag is not supported on GCC 4.8. If you want to use C++14 features you need to compile with -std=c++1y. Using godbolt.orgit appears that the earilest version to support -std=c++14is GCC 4.9.0 or Clang 3.5.0

-std=c++14GCC 4.8 不支持该标志。如果要使用 C++14 功能,则需要使用-std=c++1y. 使用Godbolt.org似乎支持的最早版本-std=c++14是 GCC 4.9.0 或 Clang 3.5.0

回答by sepp2k

G++ does support C++14 both via -std=c++14and -std=c++1y. The latter was the common name for the standard before it was known in which year it would be released. In older versions (including yours) only the latter is accepted as the release year wasn't known yet when those versions were released.

G++ 确实通过-std=c++14和支持 C++14 -std=c++1y。后者是标准的通用名称,之后才知道它将在哪一年发布。在旧版本(包括你的)中,只有后者被接受,因为发布这些版本时还不知道发布年份。

I used "sudo apt-get install g++" which should automatically retrieve the latest version, is that correct?

我使用了“sudo apt-get install g++”,它应该会自动检索最新版本,对吗?

It installs the latest version available in the Ubuntu repositories, not the latest version that exists.

它安装 Ubuntu 存储库中可用的最新版本,而不是现有的最新版本。

The latest GCC version is 5.2.

最新的 GCC 版本是 5.2。

回答by WillC

Follow the instructions at https://gist.github.com/application2000/73fd6f4bf1be6600a2cf9f56315a2d91to set up the gcc version you need - gcc 5 or gcc 6 - on Ubuntu 14.04. The instructions include configuring update-alternativesto allow you to switch between versions as you need to.

按照https://gist.github.com/application2000/73fd6f4bf1be6600a2cf9f56315a2d91上的说明在Ubuntu 14.04 上设置您需要的 gcc 版本 - gcc 5 或 gcc 6。这些说明包括配置update-alternatives以允许您根据需要在版本之间切换。