Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using a g++ 4.7 on Ubuntu 12.04 to compile my c++ code, and it won't be compiled unless an -std=gnu++11 option is added to my command like this:

g++ -std=gnu++11 main.cpp

Is there a way to add this option permanently to the compiler, because my text editor uses g++ to automatically compile my code and it won't work because I have to add this option and I can't add it like I can normally do inside of the terminal?

I've seen that people use some spec file to configure g++, but I don't understand how to do it, so the help from you guys will be appreciated.

share|improve this question
Spec file? That might be make with a makefile. A makefile is a horribly convoluted thing that I won't pretend to understand, though some people do, somehow. – Bob Jul 16 '12 at 11:37
@Bob: A Makefile is a rather simple thing that is easy to understand once you get the idea of it being functional, not procedural. It's also mighty helpful in any kind of development. I wrote a tutorial about it which might be helpful for you... – DevSolar Jul 16 '12 at 11:43
@DevSolar Yea, I was kinda joking there. Personally, I've never had a reason to learn about them (IDEs! :D), but the people who bother can probably pick it up quite quickly. – Bob Jul 16 '12 at 11:46
Note that spec file and Makefile are almost completely unrelated things: while the former is a compiler configuration file usually shipped with your compiler, the latter is a build instruction file shipped with your project. For the latter, export CXXFLAGS=-std=gnu++11 would usually suffice, but that's not the question at hand I believe. – MvG Jul 16 '12 at 13:02
up vote 1 down vote accepted

I would simply create a shell script ~/bin/g++ containing the lines

exec /usr/bin/g++ -std=gnu++11 "$@"

and ensure that ~/bin is on the PATH, and listed there before any other directory which contains a g++ executable (usually thats's /usr/bin). Editing the spec files is possible, but for your settings to become default, I believe you'd have to change the default specs, which means it will require root access and severe tinkering with the gcc internals. There is a good chance that this tinkering would be undone by the next upgrade of g++.

share|improve this answer
This also requires that ~/bin first in $PATH, at least before the directory where the real g++ lives. I'd suggest alias g++='g++ --std=gnu++11' instead. – ott-- Jul 16 '12 at 12:51
@ott, alias is all right for interactive command line use, but usually won't affect a command invoked by some editor, as OP requires. – MvG Jul 16 '12 at 13:00
Ok, from within an editor the script is the right way. – ott-- Jul 16 '12 at 13:38
As @ott correctly pointed out, your dir has to come before the established ones. Edited the question. Also note that the ~ in my answer refers to your home directory, so you should write export PATH=${HOME}/bin:${PATH}. Putting personal stuff in /bin is a bad idea, and should have required root access which my answer claims to be unneccessary. – MvG Jul 16 '12 at 14:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.