Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch between different GCC versions

Tags:

c

linux

gcc

cuda

I recently built an older version of GCC and installed it in my home directory (spec. ~/local/gcc-5.3.0). However, I need this compiler only for CUDA projects, and will be working with the system compiler (GCC 6.2.1) rest of the time. So, I guess I need to find a way to switch between these as and when needed, and in a way that also changes the library and include paths appropriately.

I understand that update-alternatives is one way to do so, but it seems to require root permissions to be set up, which I don't have.

The next best thing might be to write a shell function in .bashrc that ensures the following:

  • Each call switches between system and local gcc

  • Whenever a switch is made, it adjusts paths so that when local gcc is chosen, it first looks for header files and libraries that were installed by itself before looking in system paths like /usr/local/include or usr/local/lib. A previous answer suggests that modifying LD_LIBRARY_PATH should be sufficient, because a GCC installation "knows" where its own header files and static libraries are (I am not sure if it's correct, I was thinking I might need to modify CPATH, etc).

Is the above the best way to achieve this? If so, what paths should I set while implementing such a function?

like image 908
oczkoisse Avatar asked Oct 17 '22 20:10

oczkoisse


1 Answers

Is the above the best way to achieve this? If so, what paths should I set while implementing such a function?

As others pointed out, PATH and LD_LIBRARY_PATH are mandatory. You may also update MANPATH for completeness.

Rather than reinventing the wheel in .bashrc I suggest to employ a little known but extremely handy and modular Environment Modules that were designed for this specific purpose. You could use them like (once you set up config for gcc/3.1.1):

$ module load gcc/3.1.1
$ which gcc
/usr/local/gcc/3.1.1/linux/bin/gcc
$ module unload gcc
$ which gcc
gcc not found
like image 84
yugr Avatar answered Nov 04 '22 01:11

yugr