6

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?

Community
  • 1
  • 1
oczkoisse
  • 1,561
  • 3
  • 17
  • 31
  • My personal approach to this is not very sophisticated: I have a shell script that just adds the CUDA bin dir and the matching gcc bin dir to $PATH. Actually I have multiple scripts to keep different CUDA versions around. When I stop working with CUDA or want to switch to a different version, I open a new shell. – tera Dec 19 '16 at 01:45
  • You mention adding only the `bin` directory of gcc to `PATH`. Isn't it also necessary to modify `LD_LIBRARY_PATH` if not the include directories? – oczkoisse Dec 19 '16 at 01:56
  • 1
    Both nvcc and gcc know where their libraries are located, so just changing $PATH is indeed sufficient. – tera Dec 19 '16 at 01:59
  • if this is only for nvcc, then you probably only need the ccbin option in nvcc – talonmies Dec 19 '16 at 06:16
  • Wouldn't CMake easily deal with this? – Matthew Schuchard Dec 19 '16 at 12:47

1 Answers1

3

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
yugr
  • 19,769
  • 3
  • 51
  • 96
  • Seems like a pretty nifty tool to configure shell environment variables. Now just need to figure out how to actually write a module file for my purpose. Thanks for your help. – oczkoisse Dec 30 '16 at 03:28