15

I have downloaded g++ 4.8 on Ubuntu 12.10 by doing:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-4.8

but when I do:

sudo update-alternatives --config g++

to switch g++ versions it says:

update-alternatives: error: no alternatives for g++.

However if I do:

g++ --version

it says:

g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

How do I set my g++ 4.8 installation to be the default g++?

0

3 Answers 3

18

You need to let update-alternatives to know that you have 2 C++ compilers, create a record for each one, and then configure which one you want to use. This is done with the following:

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6.3 60
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 40
sudo update-alternatives --config g++ 

From this point forward, the only thing required when switching compilers is this command:

sudo update-alternatives --config g++

See man update-alternatives for more info.

Source: How to use multiple instances of gcc?

7
  • @Braiam It can be done in the same way with any package. Nov 7, 2013 at 0:08
  • 3
    Can someone elaborate on the 60 and 40? Ive seen this in lots of places
    – user997112
    Nov 7, 2013 at 0:09
  • @user997112 I suggest you to read the man, but, if you like more 70 and 30, then use 70 and 30... Nov 7, 2013 at 0:10
  • 1
    @user997112 these are priority numbers, and are only relevant when using --auto. you can see some examples using update-alternatives --display editor
    – Braiam
    Nov 7, 2013 at 0:14
  • 2
    On 12.04 I get: update-alternatives: error: alternative g++ can't be master: it is a slave of gcc Jan 9, 2015 at 16:22
11

First of all, you must discover where are your 4.8 binaries of all the tools:

$ which gcc-4.8
/usr/bin/gcc-4.8
$ which g++-4.8
/usr/bin/g++-4.8
$ ls /usr/bin/*-4.8
/usr/bin/cpp-4.8         /usr/bin/x86_64-linux-gnu-cpp-4.8
/usr/bin/g++-4.8         /usr/bin/x86_64-linux-gnu-g++-4.8
/usr/bin/gcc-4.8         /usr/bin/x86_64-linux-gnu-gcc-4.8
/usr/bin/gcc-ar-4.8      /usr/bin/x86_64-linux-gnu-gcc-ar-4.8
/usr/bin/gcc-nm-4.8      /usr/bin/x86_64-linux-gnu-gcc-nm-4.8
/usr/bin/gcc-ranlib-4.8  /usr/bin/x86_64-linux-gnu-gcc-ranlib-4.8
/usr/bin/gcov-4.8

So, we have all our binaries, now lets see if some symlinks are available for such binaries:

$ cd /usr/bin
$ ls -l gcc* cpp g++
lrwxrwxrwx 1 root root      7 sep 18 14:02 cpp -> cpp-4.7
lrwxrwxrwx 1 root root      7 abr 22  2013 g++ -> g++-4.7
lrwxrwxrwx 1 root root      7 sep 18 14:02 gcc -> gcc-4.7

As we can see, only cpp, g++ and gcc has symbolic links. We have two options here.

Symlinking

We replace the symlinks with ours, removing the actuals first:

sudo rm /usr/bin/cpp /usr/bin/gcc /usr/bin/g++

Then creating ours

sudo ln -s /usr/bin/cpp-4.8 /usr/bin/cpp
sudo ln -s /usr/bin/gcc-4.8 /usr/bin/gcc
sudo ln -s /usr/bin/g++-4.8 /usr/bin/g++

To revert it back use the same commands but with 4.7 or 4.6 instead

sudo rm /usr/bin/cpp /usr/bin/gcc /usr/bin/g++
sudo ln -s /usr/bin/cpp-4.7 /usr/bin/cpp
sudo ln -s /usr/bin/gcc-4.7 /usr/bin/gcc
sudo ln -s /usr/bin/g++-4.7 /usr/bin/g++
5
  • When I did: ls -l gcc* cpp g++ it returned: ls: cannot access gcc*: No such file or directory ls: cannot access cpp: No such file or directory ls: cannot access g++: No such file or directory
    – user997112
    Nov 6, 2013 at 23:37
  • No apology required :) Once I call the remove function it works, but when I go to create "our" symlinks it says: sudo ln -s -T /usr/bin/cpp /usr/bin/cpp-4.8 ln: failed to create symbolic link `/usr/bin/cpp-4.8': File exists
    – user997112
    Nov 7, 2013 at 0:01
  • (I called the rm command twice just so on the second time it confirmed/complained that the existing links were in fact deleted)
    – user997112
    Nov 7, 2013 at 0:02
  • 3
    This is a terrible idea. There are many programs who use gcc to compile kernel modules and some of them (e.g. Nvidia) will silently crash when the gcc they call will not be the one the kernel was compiled with. Aug 27, 2015 at 11:33
  • 1
    @nbubis well, the question is "How do I set my g++ 4.8 installation to be the default g++?", so, one should presume whoever does this, they know what they are doing, no? On the other hand, what alternative can you propose?
    – Braiam
    Aug 27, 2015 at 13:37
10

This is the great description and step-by-step instruction how to create and manage master and slave (gcc and g++) alternatives.

Shortly, it's

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7 
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8 
sudo update-alternatives --config gcc
3
  • Welcome to Ask Ubuntu! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – Stormvirux
    Mar 13, 2014 at 2:52
  • You're right. I added the essential part
    – Anton K
    Mar 13, 2014 at 18:28
  • 1
    Saved my day with that slave stuff. tx Mar 19, 2016 at 23:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .