User:Tsu2/gcc update-alternatives

Jump to: navigation, search

GCC Update-alternatives

Practically all distros make multiple versions of gcc available in their repositories.
Practically no distro (including openSUSE as of today) provides instruction or method for installing multiple gcc side by side and choosing which one to use when needed.

Update Alternatives

Many distros today use update-alternatives to switch between various versions of binaries and library trees. Probably the commonly seen is to switch between openjdk and Oracle Java, and versions of Java.

Prerequisites

There are no real prerequisites, but for this article, I highly recommend using the "locate" utility to quickly find files instead of "Find" which is installed by default on openSUSE. To install "locate," you will need to install the "mlocate" package as follows

zypper in mlocate

Followed by updating its database immediately. The database updates naturally every 24 hrs but we don't want to wait a day for the database to update itself. To use "locate" immediately run the following command to update the database

updatedb

Tumbleweed today (as an example)

Today, gcc6 is installed by default, while gcc5 is available to be installed as well. You can install gcc5 side by side in Tumbleweed with the following command (similar commands for versions of gcc exist for other versions of openSUSE)

zypper in gcc5

The binaries for gcc5 and gcc6 are located in /usr/bin.
We can verify this by running the locate utility

locate gcc

/usr/bin/gcc
/usr/bin/gcc-5
/usr/bin/gcc-6
/usr/bin/gcc-ar
/usr/bin/gcc-ar-5
/usr/bin/gcc-ar-6
/usr/bin/gcc-nm
/usr/bin/gcc-nm-5
/usr/bin/gcc-nm-6
/usr/bin/gcc-ranlib
/usr/bin/gcc-ranlib-5
/usr/bin/gcc-ranlib-6

Unless you have a special reason to use any of the other binaries, we're only interested in

/usr/bin/gcc-5
/usr/bin/gcc-6

If you want the additional gcc options, go ahead and create more based on the following two commands which can be run individually or placed in a script. Note that whatever you configure first will be the default(although you can change later if you make this minor mistake)

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 50
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 20

Configuring GCC and Update-alternatives

Now you can test your work. Run the following to view or change, the following also displays the result

update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-6   50        auto mode
  1            /usr/bin/gcc-5   20        manual mode
  2            /usr/bin/gcc-6   50        manual mode

Press <enter> to keep the current choice[*], or type selection number:

You can always check your currently configured version with the following command

gcc --version