How to temporarily change default version of gcc in ArchLinux

I have ArchLinux and 2 versions of gcc installed on it. default one (“gcc”) which is “gcc (GCC) 7.1.1” and “gcc-5” one which is “gcc-5 (GCC) 5.4.0”.

I have a hard time building some projects that check the version of gcc (and g++) to be less than or equal to 5. (to be more specific installing official run-file of cuda-8.0 from nvidia, or building projects which have used cuda library and its header files)

all I want is to change default version of gcc on my environment (temporarily) and set it back later when i’m done, something like “update-alternatives” solution in debian-like distros.

here is some information about binaries related to gcc and g++ at my /usr/bin :

$ ls -l | grep gcc
lrwxrwxrwx 1 root root        3 Jun 30 16:49 cc -> gcc
-rwxr-xr-x 3 root root   997840 Jun 30 16:49 gcc
-rwxr-xr-x 3 root root   873352 Jul 11  2016 gcc-5
-rwxr-xr-x 2 root root    27104 Jun 30 16:49 gcc-ar
-rwxr-xr-x 2 root root    25544 Jul 11  2016 gcc-ar-5
-rwxr-xr-x 2 root root   875720 Jul 11  2016 gccgo-5
-rwxr-xr-x 2 root root    27104 Jun 30 16:49 gcc-nm
-rwxr-xr-x 2 root root    25512 Jul 11  2016 gcc-nm-5
-rwxr-xr-x 2 root root    27104 Jun 30 16:49 gcc-ranlib
-rwxr-xr-x 2 root root    25512 Jul 11  2016 gcc-ranlib-5
-rwxr-xr-x 3 root root   997840 Jun 30 16:49 x86_64-pc-linux-gnu-gcc
-rwxr-xr-x 3 root root   873352 Jul 11  2016 x86_64-pc-linux-gnu-gcc-5
-rwxr-xr-x 3 root root   873352 Jul 11  2016 x86_64-pc-linux-gnu-gcc-5.4.0
-rwxr-xr-x 3 root root   997840 Jun 30 16:49 x86_64-pc-linux-gnu-gcc-7.1.1
-rwxr-xr-x 2 root root    27104 Jun 30 16:49 x86_64-pc-linux-gnu-gcc-ar
-rwxr-xr-x 2 root root    25544 Jul 11  2016 x86_64-pc-linux-gnu-gcc-ar-5
-rwxr-xr-x 2 root root   875720 Jul 11  2016 x86_64-pc-linux-gnu-gccgo-5
-rwxr-xr-x 2 root root    27104 Jun 30 16:49 x86_64-pc-linux-gnu-gcc-nm
-rwxr-xr-x 2 root root    25512 Jul 11  2016 x86_64-pc-linux-gnu-gcc-nm-5
-rwxr-xr-x 2 root root    27104 Jun 30 16:49 x86_64-pc-linux-gnu-gcc-ranlib
-rwxr-xr-x 2 root root    25512 Jul 11  2016 x86_64-pc-linux-gnu-gcc-ranlib-5
Asked By: salehjg

||

TLDR: sudo ln -s $(which gcc-5) /usr/local/bin/gcc; hash -r

By default, the gcc binary is located in /usr/bin

Checking our path, we see that /usr/local/bin comes first:

echo $PATH | tr ':' 'n'
/usr/local/bin
/usr/bin
/bin
/usr/local/sbin
. . .

So, assuming we installed gcc-5 (yaourt --noconfirm -S gcc5) we can make that the “default” version by creating a symlink in /usr/local/bin like so:

sudo ln -s $(which gcc-5) /usr/local/bin/gcc
sudo ln -s $(which g++-5) /usr/local/bin/g++

Next, we need to rehash gcc to remove the old, remembered gcc location:

hash -r

To restore the original “default” version of gcc we just need to delete that symlink and rehash:

sudo rm /usr/local/bin/gcc /usr/local/bin/g++
hash -r
Answered By: l3x
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.