9

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
4
  • What happens if you set the environment variable CC=gcc-5 when building the project? (And likewise for CXX=whateverg++5iscalledhere)
    – thrig
    Aug 8, 2017 at 17:42
  • sometimes this solution doesn't work, especially on projects like opencv or... with dedicated cmake scripts to detect gcc version. (or I'm using it wrong)
    – salehjg
    Aug 8, 2017 at 17:47
  • 3
    Almost all autoconf-based build systems will honor PATH. Create a directory $HOME/oldstuff, symlink gcc in that directory to /usr/bin/gcc-5. Add that directory to the front of PATH before building software that requires the old gcc. Aug 8, 2017 at 23:48
  • @MarkPlotnick : looks like a neat solution, thank you. for future readers: you can override compiler version checking using --override : "cuda-<version>.run --override" (i just found it)
    – salehjg
    Aug 9, 2017 at 0:00

1 Answer 1

10

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
2
  • Is it ok to set icc and icpc this way? will cmake and other app will use this directly by just doing this? Mar 15, 2019 at 9:10
  • I also needed to set these shell variables: bash export CC=/usr/local/bin/gcc export CXX=/usr/local/bin/g++
    – Bob
    Jun 5, 2023 at 19:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .