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.
Asked
Active
Viewed 8.4k times
24
-
9`g++ -std=c++11` or if that doesn't work, `g++ -std=c++0x` – jxh Jun 29 '13 at 09:59
-
3You can always install another version of GCC alongside the system one. You can even compile another version on your own. There are many tutorials in the net - just use your preferred search engine. GCC usually suffixes its executables with the version number and also installs its specific libraries in versioned directories so newer versions won't clash with the system-provided one. – Hristo Iliev Jun 29 '13 at 10:18
2 Answers
27
As others have suggested, you need to enter the std commandline option. Let us make it easy for you
- 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
Thats it. By default it will compile for c++11 standard.
NOTE: If you follow the above mentioned option, to compile non-c++ 11 programs, you have to use
g++ --std=c++98 filename.cpp

thefourtheye
- 233,700
- 52
- 457
- 497
-
ok. Thanks. But in the last note you mentioned that to compile non c++11 programs change the command. But isn't every version backward compatible. So c++11 compiler should compile even a program without c++11 specific commands. – Karan Talasila Jun 29 '13 at 10:41
22
gcc 4.6.3 supports many c++11 features. However, they are disabled by default. To enable them, use the following flag:
g++ -std=c++0x ...
This flag also disables GNU extensions; to keep them enabled, use -std=gnu++0x
flag.

Sergey Kalinichenko
- 714,442
- 84
- 1,110
- 1,523
-
so i need to enter command for compiling as g++ -o executable filename.cc -std=c++0x is it? – Karan Talasila Jun 29 '13 at 10:04
-
1@talasila Correct. You may want to consider upgrading gcc to 4.8, [it has much better c++11 support](http://gcc.gnu.org/gcc-4.8/cxx0x_status.html). – Sergey Kalinichenko Jun 29 '13 at 10:05
-
how do i update my default compiler to a new one. why do people say not to remove an existing version of gcc in operating system as it makes the system unstable? Is it true? – Karan Talasila Jun 29 '13 at 10:08
-
@talasila Here is [a link to an answer with the details on upgrading the compiler](http://askubuntu.com/a/271561). I have no idea why people say that upgrading the compiler somehow makes the system unstable. You may want to ask on `askubuntu.com`, they may have a better answer. – Sergey Kalinichenko Jun 29 '13 at 10:21
-
After many many articles saying to upgrade gcc in order to get c++11 functionality, everyone failed to mention that this functionality wasn't enabled by default. Thank you! – beckah Feb 11 '16 at 16:57