4

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
  • What happens if you set the environment variable CC=gcc-5 when building the project? (And likewise for CXX=whateverg++5iscalledhere) – thrig Aug 8 '17 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) – saleh jamali Aug 8 '17 at 17:47
  • 2
    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. – Mark Plotnick Aug 8 '17 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) – saleh jamali Aug 9 '17 at 0:00
2

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
  • Is it ok to set icc and icpc this way? will cmake and other app will use this directly by just doing this? – Love Grover Mar 15 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.