1

I have just installed gcc 4.7 from ppa repository. Ubuntu 12.04 already comes with 4.6. When I use command:

gcc --version

It tells me it is 4.6.3 to be exact.

I have no idea of how to launch the 4.7 compiler. In fact, I wouldn't mind erasing the former version and stay with 4.7 only (I guess that wouldn't be a problem).

How can I fix this?

4
  • do you have synaptic on your system?!
    – user61928
    Sep 27, 2012 at 8:40
  • If i uninstall gcc from synaptic, when I call gcc into console version 4.7 will show up? Just as easy as that?
    – Roman Rdgz
    Sep 27, 2012 at 9:05
  • yes but you must check which version you are removing
    – user61928
    Sep 27, 2012 at 9:08
  • you have to remove you old gcc first sudo apt-get remove gcc* then install the newer version
    – blade19899
    Sep 27, 2012 at 10:10

2 Answers 2

0

Use update-alternatives (from CLI) or galternatives (from GUI) to set the default for gcc.

If you don't have them, install with: sudo apt-get install galternatives.
I would use the GUI version, it is more simple for a beginner.

From CLI:
sudo update-alternatives --install /usr/bin/gcc gcc /path/to/newer/gcc 40
sudo update-alternatives --install /usr/bin/gcc gcc /path/to/older/gcc 30
sudo update-alternatives --config gcc

4
  • I have installed galternatives but I have no idea of what I should do with it. gcc is not listed among the list of alternatives. 'C++' is, for example, and if I click I see only one option, using /usr/bin/g++. But I don't know If adding a new option to the newer g++
    – Roman Rdgz
    Sep 27, 2012 at 10:59
  • See the CLI commands. Customize them, to fit your needs.
    – Frantique
    Sep 27, 2012 at 12:31
  • I have had a look at /usr/bin looking for the newer g++, and I found that I currently have gcc4.5, 4.6 and 4.7, and then there is a link to executable called gcc, which points to gcc-4.6. Same for g++. Wouldn't it be easier to modify this link from console (don't know how to) to point to 4.7, and leave everything else as it is?
    – Roman Rdgz
    Sep 27, 2012 at 13:17
  • Roman: for this is made the alternatives to not to modify anything in the filesystem. Try to read a little bit about it, it worths...
    – Frantique
    Sep 27, 2012 at 13:25
0

There are mechanisms in place to switch the version of gcc (and other tools) you get with using their name (gcc), See the update-alternatives man page, but there are recommendations that changing the system-wide gcc default is a bad thing. The existing programs have been successfully compiled and tested with the default compiler, and while it would be hoped that a newer compiler would still produce acceptable results, there are no guarantees. Things that are deprecated in a compiler may be removed in the next version. New bugs may be introduced. There's a reason a distribution uses a particular compiler version -- the programs they provide all work with that version, and some probably do not work with a later version.
For you own use, choose the compiler you want (gcc-8 in the below example) without altering the system-wide defaults by adding the gcc ( and ar, nm, ranlib) links to the desired version into your own ~/bin directory:

ln -s ~/bin/gcc -> /usr/bin/gcc-8  
ln -s ~/bin/ar -> /usr/bin/gcc-ar-8  
ln -s ~/bin/ranlib -> /usr/bin/gcc-ranlib-8  
ln -s ~/bin/nm -> /usr/bin/gcc-nm-8  

If your ~/bin directory does not exist, make it:

mkdir ~/bin

Your ~/bin will be picked up and added to the beginning of your PATH environment variable, so that the gcc command will be found there and used, instead of the system default in /usr/bin.

This technique may be applied to applications which need specific compiler versions (CUDA for example). Links to the selected compiler version may be put into the application bin directory, and that location may be added first to your PATH (in .profile for bash shell users).

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