如何在 ubuntu 上将 gcc 编译器更改为 c++11
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17378969/
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
how to change gcc compiler to c++11 on ubuntu
提问by Karan Talasila
I use ubuntu 12.04 and the default gcc is 4.6.3. It is not accepting c++11 commands and is giving me output saying the command is not c++98 compatible. I checked online and have seen people advising to not change default compilers on operating system as it becomes unstable. Can anybody suggest a fix or a safe way of downloading a gcc compiler that is c++11 compliant.
我使用 ubuntu 12.04,默认的 gcc 是 4.6.3。它不接受 c++11 命令,并给我输出说该命令与 c++98 不兼容。我在网上查了一下,看到有人建议不要更改操作系统上的默认编译器,因为它变得不稳定。任何人都可以建议下载符合 c++11 的 gcc 编译器的修复或安全方法。
采纳答案by dasblinkenlight
gcc 4.6.3 supports many c++11 features. However, they are disabled by default. To enable them, use the following flag:
gcc 4.6.3支持许多 c++11 特性。但是,默认情况下它们是禁用的。要启用它们,请使用以下标志:
g++ -std=c++0x ...
This flag also disables GNU extensions; to keep them enabled, use -std=gnu++0x
flag.
此标志还禁用 GNU 扩展;要保持启用状态,请使用-std=gnu++0x
标志。
回答by thefourtheye
As others have suggested, you need to enter the std commandline option. Let us make it easy for you
正如其他人所建议的,您需要输入 std 命令行选项。让我们为您轻松
- Open terminal by pressing Ctrl+Alt+T
sudo gedit ~/.bashrc
Enter the following line as the last line
alias g++="g++ --std=c++0x"
- Save and close the file and close the terminal.
- Now open terminal again and compile your c++ 11 programs simply by
g++ filename.cpp
- 按Ctrl+ Alt+打开终端T
sudo gedit ~/.bashrc
输入以下行作为最后一行
alias g++="g++ --std=c++0x"
- 保存并关闭文件并关闭终端。
- 现在再次打开终端并简单地编译你的 C++ 11 程序
g++ filename.cpp
Thats it. By default it will compile for c++11 standard.
就是这样。默认情况下,它将为 c++11 标准编译。
NOTE:If you follow the above mentioned option, to compile non-c++ 11 programs, you have to use
注意:如果您遵循上述选项,要编译非 C++ 11 程序,您必须使用
g++ --std=c++98 filename.cpp