Karan Talasila
Karan Talasila

Reputation: 459

how to change gcc compiler to c++11 on ubuntu

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.

Upvotes: 24

Views: 87147

Answers (2)

thefourtheye
thefourtheye

Reputation: 239443

As others have suggested, you need to enter the std commandline option. Let us make it easy for you

  1. Open terminal by pressing Ctrl+Alt+T
  2. sudo gedit ~/.bashrc
  3. Enter the following line as the last line

    alias g++="g++ --std=c++0x"
    
  4. Save and close the file and close the terminal.
  5. 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

Upvotes: 28

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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.

Upvotes: 22

Related Questions