Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

So I have installed gcc-4.4 and gcc-4.3 (same for g++). Now as far as I remember there is a tool in Ubuntu which sets the symlinks for you if you just tell it which version you want. However it does not seem to work in the newest version, which I find disappointing.

root@nexus:~# update-alternatives --config gcc
update-alternatives: error: no alternatives for gcc.
root@nexus:~# update-alternatives --config cc
There is only one alternative in link group cc: /usr/bin/gcc
Nothing to configure.


root@nexus:~# dpkg -l | grep gcc | awk '{print $2}'
gcc
gcc-4.3
gcc-4.3-base
gcc-4.3-multilib
gcc-4.4
gcc-4.4-base
gcc-4.4-multilib
gcc-4.5-base
gcc-multilib
lib32gcc1
libgcc1

Any ideas?

share|improve this question

3 Answers 3

up vote 44 down vote accepted

First erased the current update-alternatives setup for gcc and g++:

sudo update-alternatives --remove-all gcc 
sudo update-alternatives --remove-all g++

Install Packages

It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:

sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4

Install Alternatives

Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20

sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc

sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++

Configure Alternatives

The last step is configuring the default commands for gcc, g++. It's easy to switch between 4.3 and 4.4 interactively:

sudo update-alternatives --config gcc
sudo update-alternatives --config g++

Or switch using script:

#!/bin/sh

if [ -z "$1" ]; then
    echo "usage: $0 version" 1>&2
    exit 1
fi

if [ ! -f "/usr/bin/gcc-$1" ] || [ ! -f "/usr/bin/g++-$1" ]; then
    echo "no such version gcc/g++ installed" 1>&2
    exit 1
fi

update-alternatives --set gcc "/usr/bin/gcc-$1"
update-alternatives --set g++ "/usr/bin/g++-$1"
share|improve this answer
2  
Thx, so you have to add them manually to update-alternatives.. IIRC earlier Ubuntu versions did that automatically. –  Nils Feb 15 '11 at 19:33
    
This is especially useful to me when compiling nvidia modules for different kernels. Thank you so much for explaining update-alternatives –  earthmeLon Apr 15 '12 at 10:57
    
Thank you! I used your other answer to update from 4.6 to 4.7. I wanted to use this answer, but I was not sure why do you put numbers like 10 20 30 after some of you commands. Could you please explain? –  Martin Drozdik Dec 28 '12 at 22:02
5  
The numbers are priorities as it turns out, according to the manpage. I imagine in the case that one of the version is uninstalled it will use those priorities to determine which one should become the new default. –  Ibrahim Jan 14 '13 at 20:04
    
g++ config command doesn't work? –  mezamorphic Nov 3 '13 at 20:01

execute in terminal :

gcc -v
g++ -v

Okay, so that part is fairly simple. The tricky part is that when you issue the command GCC it is actually a sybolic link to which ever version of GCC you are using. What this means is we can create a symbolic link from GCC to whichever version of GCC we want.

  • You can see the symbolic link :
ls -la /usr/bin | grep gcc-4.4
ls -la /usr/bin | grep g++-4.4
  • So what we need to do is remove the GCC symlink and the G++ symlink and then recreate them linked to GCC 4.3 and G++ 4.3:
rm /usr/bin/gcc
rm /usr/bin/g++

ln -s /usr/bin/gcc-4.3 /usr/bin/gcc
ln -s /usr/bin/g++-4.3 /usr/bin/g++
  • Now if we check the symbolic links again we will see GCC & G++ are now linked to GCC 4.3 and G++ 4.3:
ls -la /usr/bin/ | grep gcc
ls -la /usr/bin/ | grep g++
  • Finally we can check our GCC -v again and make sure we are using the correct version:
gcc -v
g++ -v
share|improve this answer
    
thank you. Very handy way! –  ruX Apr 14 '13 at 13:20

Is this really desirable? There are ABI changes between gcc versions. Compiling something with one version (eg the entire operating system) and then compiling something else with another version, can cause conflict.

For example, kernel modules should always be compiled with the same version of gcc used to compile the kernel. With that in mind, if you manually altered the symlink between /usr/bin/gcc and the version used in your version of Ubuntu, future DKMS-built modules might use the wrong gcc version.

If you just want to build things with a different version of gcc, that's easy enough, even with makescripts. For example, you can pass in the version of gcc in the CC environment variable:

CC="gcc-4.5" ./configure
CC="gcc-4.5" make

You might not need it on the make command (configure scripts usually pull it in) but it doesn't hurt.

share|improve this answer
    
Thx for your comment. I'm aware of the CC variable, but this was not really the question. –  Nils Feb 15 '11 at 16:31
    
True enough but I have explained why gcc isn't part of the alternatives system and why this isn't particularly desirable. If neither of those are going to change your mind, just do it manually. –  Oli Feb 15 '11 at 16:43
    
It was before? Now they just removed it?! Compiling (userspace) software with different gcc versions should be perfectly ok.. this discussion is getting sill.. –  Nils Feb 15 '11 at 18:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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