Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 965
Karan Talasila Avatar asked Jun 29 '13 09:06

Karan Talasila


People also ask

Does GCC support C++11?

According to cppreference, full support of c++11 came with gcc 4.8. 1; To have full support of c++14 (with some of the new features of c++17), instead, you need gcc 5.0 and above.

How do I enable C++11 mode in my compiler?

You need to add the option --std=c++11 (not c+11) to the compiler's command line, which tells the compiler to use the STanDard language version called C++11.

How do you specify STD C++11 in your Makefile?

You can use command-line flag -std to explicitly specify the C++ standard. For example, -std=c++98 , or -std=gnu++98 (C++98 with GNU extensions) -std=c++11 , or -std=gnu++11 (C++11 with GNU extensions)


2 Answers

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 
like image 146
thefourtheye Avatar answered Oct 24 '22 06:10

thefourtheye


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.

like image 39
Sergey Kalinichenko Avatar answered Oct 24 '22 05:10

Sergey Kalinichenko