Swapping g++/gcc compiler versions

Update
Nikos emailed me some time back and told me about an alternative way on Ubuntu/debian systems called update-alternatives. I have not gotten around to try it out myself, because I have not had any need for it. Posting part of his email below if anyone want to give it a shot however:
  There is another debian/ubuntu specific fix that is very handy (imho).
  There is a debian utility named update-alternatives which does exatly what you 
  descibed, only it is more sophisticated!
  In case you are interested ( even though cuda 2.3 is supposed to work with 
  ubuntu 9.04):

       sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.1 60 
  --slave /usr/bin/g++ g++ /usr/bin/g++-4.1

      sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 40 
  --slave /usr/bin/g++ g++ /usr/bin/g++-4.3

  then you can use sudo update-alternatives --config gcc to choose which version 
  maps to "gcc"

  Original post:

I have been doing CUDA development under GNU/Linux for over a year now, and it works remarkably well. There is one small issue that may confuse developers however; the Nvidia CUDA SDK currently can not handle gcc versions higher than 4.1 . The both newer Ubuntu (I am running Kubuntu) releases - 8.04 and 8.10 have higher versions (4.2 and 4.3 respectively) installed.

There is probably some good page about out how to make the system compile using the right compiler and libc somewhere or some switch to set, but at least I could not find it when installing 8.04 so I had to figure it out back then. As I had to rewrite my script after performing a clean 8.10 install a couple of weeks back I thought it might be handy to post it here. It is a rather straight forward process.

First one need to install gcc4.1 (and g++ if developing in C++) as well as the proper libc version. This can be done using any of the apt tools avaliable in any ubuntu distro, and is fairly straight forward. (Just install the g++4.1 development package, and the rest will follow from dependencies). Do not remove the already installed version (4.2 or 4.3) however, as they can happily coexist and you may want to compile some kernel module or Nvidia driver at some stage.

Second , we must make sure that the right gcc version is used. In usr/bin there are three symbolic links that needs to be changed. gcc, cpp, and g++. If you perform ls of one of the files, like

  $ ls -l /usr/bin/gcc
  lrwxrwxrwx 1 root root 16 2008-11-03 14:14 /usr/bin/gcc -> /usr/bin/gcc-4.3

you can see that it is a link to gcc-4.3 (a binary file). Same thing goes for cpp and g++ . There should also be binary files for the corresponding 4.1 version (e.g gcc-4.1). One way of changing the compiler version used by the system is simply a matter of removing this link and making a new one to the proper executable. As it may come in handy to change back (I tend to want to use the newest g++ when developing non CUDA software) and forth, a couple of simple shell scripts will come in handy. To set the compiler version to 4.1 simply put the following in a file, and save it in som convenient location (I save in /usr/bin for simplicity). I call my file setcompiler4.1.sh but you can of course choose your own names.

  #!/bin/bash
  rm /usr/bin/cpp
  ln -s /usr/bin/cpp-4.1 /usr/bin/cpp
  echo ln -s cpp-4.1 cpp
  rm /usr/bin/gcc
  ln -s /usr/bin/gcc-4.1 /usr/bin/gcc
  echo ln -s gcc-4.1 gcc
  rm /usr/bin/g++
  ln -s /usr/bin/g++-4.1 /usr/bin/g++
  echo ln -s g++-4.1 g++

If you are not comfortable with shell scripting (I am but a rookie myself), rm removes the link, ln creates a link (-s makes it a soft link) and echo merely echoes the argument (I do this just to remind myself what is happening).

Put a similar thing for 4.2 and, or 4.3 in its own file, so that you easily can reset, and make sure that the shell scripts are executable

  sudo chmod +x set_compiler_4.1.sh

  Now to set a a compiler version, simply call the script with superuser privileges

  sudo set_compiler_4.1.sh

for instance.

That is about it. Long text for a simple fix… Ah, and be careful with those rm's in superuser mode if you are new to GNU/Linux.

.L