Change default C++ standard in g++

10,994

After a bit of research (which you've probably already done yourself), I haven't found a way to change the default behavior of g++ other than rebuilding a custom version or aliasing it.


Why this is probably a good thing:

It is important that each version of g++ has a single, well-defined default behavior. Consider this: if you change the default behavior of g++ and try to compile a C++ project whose author could not be aware of your configuration, the project may fail to compile or compile with subtle errors / unexpected behaviors.

In your own project, you can easily add all the relevant flags and options to your Makefile or CMakeLists.txt so that you don't need to type them again. This will also ensure that other people compiling your project will receive the correct options regardless of their configuration.

Share:
10,994
Rohan Kumar
Author by

Rohan Kumar

I'm a young programmer from Chandigarh, India. I was introduced to programming at university. But I've also worked commercially on Javascript (Node.js) and Java. These days I'm working at Red Hat around Java tooling and libraries around Kubernetes. Email: [email protected] CV : https://stackoverflow.com/cv/rohankanojia

Updated on June 08, 2022

Comments

  • Rohan Kumar
    Rohan Kumar about 2 years

    In order to compile a program in C++11 standard we need to do :

     g++ -std=c++11 myProgram.cpp -o myProgramExec
    

    But is it possible that i can set the default standard of g++ as C++11, so that i don't have to mention this option again and again Although i can also add an alias for this in my .bashrc :

    alias g++='g++ -std=c++11';
    

    But i wonder if there is any better way than this.Is there any config file of g++ which can be edited in order to achieve this? Or is there some easier way to do this?

  • jbapple
    jbapple over 7 years
    This does not answer the question.
  • merlinND
    merlinND over 7 years
    @jbapple Edited with my best answer to the question (which is a bit disappointing).