I've not used alternatives before, and I may be doing it wrong: I'm certainly not getting the results I expect. I need to use two versions of GCC, and wanted to set them up using alternatives. Here's what I did:

$ sudo alternatives --install /usr/bin/gcc gcc /opt/gcc4.6.3/bin/gcc 20000
$ sudo alternatives --config gcc

There is 1 program that provides 'gcc'.

  Selection    Command
*+ 1           /opt/gcc4.6.3/bin/gcc

Enter to keep the current selection[+], or type selection number: 1
$ which gcc

What is going on? Why is gcc still the one installed elsewhere? Why isn't this one (which for some reason gets selected, regardless of my choice) isn't even listed as an alternative?

I'm using Fedora 17, if it matters.

  • GCC has its own mechanism for handling alternative versions, look at that too. – vonbrand Feb 15 '13 at 19:12
  • @vonbrand wouldn't you be so kind to point me at where this functionality is described? I've ensured that /usr/bin/gcc is a symlink that points to /etc/alternatives/gcc, which in turn points to the proper compiler. I've also ran updatedb just in case - all for naught, and the GCC is still found in /usr/lib64/ccache :( – wvxvw Feb 15 '13 at 19:38
  • I don't remember details, but long ago I did run the "system" gcc and a self-compiled experimental version with gcc <some-funky-flags>. – vonbrand Feb 15 '13 at 19:45
  • 1
    @vonbrand man gcc takes forever to render in Emacs :) needless to say I'm not a pro with it / am unlikely to find it, w/o knowing what exactly I'm looking for. But thanks for heads up. Interestingly, I discovered that alternatives at first, silently failed to create the symlink because the name was taken by the actual gcc binary! After handling that and re-running the whole procedure again it seems to work (I still need to try to launch an example, but I think I've "solved" it somehow already). – wvxvw Feb 15 '13 at 19:52
  • Could you post your solution as an answer and accept it please? Might help someone else. – terdon Feb 15 '13 at 21:58

Your command is, basically:

alternatives --install /usr/bin/gcc gcc /opt/gcc4.6.3/bin/gcc

This means that the alternatives system will adjust the /usr/bin/gcc symlink to point to the right binary in /opt or whatever.

Doing so will not change the path you see in which and similar commands, which just search for a binary named gcc in $PATH, and it is actually quite impossible for the alternatives system to dynamically alter $PATH of your already-running shell.

Also, you have ccache installed, which means the ccache wrapper scripts directory is put in $PATH before the usual /usr/bin, so which will always output the wrapper script's path. (The script, when run, will search for the real gcc using the same $PATH again.)

  • I've not heard before of ccache, so I didn't know what it does. Thanks for pointing me in the right direction! – wvxvw Feb 16 '13 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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