I want g++ to use -std=c++14 by default without having to type it every time. How would I do this?
2 Answers
put:
alias g++='g++ -std=c++14'
in your profile (or maybe .bashrc
). The next time you log in (or start a new terminal, with .bashrc
), the command g++
will run g++ -std=c++14
.
For a single terminal instance only, just type that on the command line. This will work in most cases, but if you have problems, try the below.
If you have scripts or programs that refer to the literal path to the g++
binary (like /usr/bin/g++
), would be to move the g++
binary to a new file name, like g++-bin
, and write a script like this named g++
in the same directory:
#! /bin/sh
/usr/bin/g++-bin -std=c++14 "$@"
This will have the same end result as an alias ($@
passes all arguments of the parent script to the g++
binary), but will make the script be used when referring to the full path to the compiler.
Be sure to make the script executable (chmod 755
or similar) if you use this method.
Also be aware that your package manager will overwrite your g++ script when g++ updates if you put the script in /usr/bin
(or your distro's equivalent), so you might want to put the script in a different directory of higher precedence in your PATH
variable.
I personally made a directory in my home dir (~/bin
) just for this kind of thing, and have lines in ~/.profile
and ~/.bashrc
that prepends $HOME/bin
to the beginning of the PATH
variable:
PATH="$HOME""/bin:""$PATH"
export PATH
-
1Note that this won't reliably work if g++ is invoked from a script or Makefile. Also, for OP's information, it would probably be wiser to name the alias something other than
g++
. The standard solution however is to use a Makefile, without a shell alias, as @JeffSchaller observes. Shell aliases are worth learning, though: they save typing time at the command line.– thbSep 12, 2016 at 0:28 -
1@thb it would be reliable if only you planned to run the script. Don't share this with others and expect it to work. And you can always un-alias it when needed with
alias g++=''
. But yeah, it's not something I do usually. If I have to type something a ton, I do though. (e.g.alias wget='wget --user-agent="Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0 Seamonkey/2.42"'
) Sep 12, 2016 at 1:40 -
You are a genius. Though, would love to know if there's new version of g++ which uses c++14 by default. Aug 22, 2018 at 12:43
-
With this solution you can't compile with older standards. eg.
g++ -std=c++11 foo.cpp
is expanded tog++ -std=c++14 -std=c++11 foo.cpp
; andgcc
/g++
, when given multiplestd
flags, will use the newest standard (of the flags given; in this casec++14
).– ElliottSep 20, 2021 at 5:07 -
@Elliott you can always run
unalias gcc
/unalias g++
for that case. Or make your wrapper script detect user-specifiedstd
values and override the default you've set. Sep 24, 2021 at 20:56
According to the C++ Standards Support in GCC page, C++14 is the default for GCC 6.1 and later:
This mode is the default in GCC 6.1 and above; it can be explicitly selected with the
-std=c++14
command-line flag, or-std=gnu++14
to enable GNU extensions as well.
In current distributions, you shouldn’t need to do anything. (GCC 6.1 was released in late April 2016.)