Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default C++ standard in g++

Tags:

c++

linux

c++11

g++

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?

like image 324
Rohan Kumar Avatar asked Jan 14 '17 10:01

Rohan Kumar


1 Answers

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.

like image 68
merlinND Avatar answered Sep 19 '22 15:09

merlinND