I want g++ to use -std=c++14 by default without having to type it every time. How would I do this?
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
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
I personally made a directory in my home dir (
~/bin) just for this kind of thing, and have lines in
~/.bashrc that prepends
$HOME/bin to the beginning of the
PATH="$HOME""/bin:""$PATH" export PATH
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++14command-line flag, or
-std=gnu++14to enable GNU extensions as well.
In current distributions, you shouldn’t need to do anything. (GCC 6.1 was released in late April 2016.)