Linux change gcc version

How to switch GCC version using update-alternatives

Many times programmers need to switch between multiple versions of gcc and cross compilers. One gcc version works well for one build, while an older or newer version of gcc is perfect for another build.

Linux’s update-alternative utility provides an easy and fantastic way to switch between the gcc version with just one line command.

“update-alternatives –install” will help you to install the alternative versions of the gcc and “update-alternatives –config” will help you to select between the available installed versions.
Let’s first take a look at what “update-alternatives” command look like:

$ update-alternatives –help
Usage: update-alternatives […]

Commands:
–install [–slave ] …
add a group of alternatives to the system.
–remove remove from the group alternative.
–remove-all remove group from the alternatives system.
–auto switch the master link to automatic mode.
–display display information about the group.
–query machine parseable version of –display .
–list display all targets of the group.
–get-selections list master alternative names and their status.
–set-selections read alternative status from standard input.
–config how alternatives for the group and ask the user to select which one to use.
–set set as alternative for .
–all call –config on all alternatives.

is the symlink pointing to /etc/alternatives/. (e.g. /usr/bin/pager)
is the master name for this link group. (e.g. pager)
is the location of one of the alternative target files. (e.g. /usr/bin/less)
is an integer; options with higher numbers have higher priority in automatic mode.

So, let’s assume we want to setup arm gcc cross compiler version 4.7 and 4.9, as two alternatives, we install both of them, so that the update-alternatives utility keeps a record for available alternatives for a .

$ sudo update-alternatives –install /usr/bin/arm-linux-gnueabi-gcc arm-linux-gnueabi-gcc /usr/bin/arm-linux-gnueabi-gcc-4.7 50

Now install the second one-

$ sudo update-alternatives –install /usr/bin/arm-linux-gnueabi-gcc arm-linux-gnueabi-gcc /usr/bin/arm-linux-gnueabi-gcc-4.9 100

Notice the numbers 50 and 100. These are just depicting the priority that we assign to each version. So a higher number means higher priority, and therefore the points to the higher priority version in automatic mode.

Now you can switch between the gcc versions using:

$ sudo update-alternatives –config arm-linux-gnueabi-gcc

Источник

Читайте также:  Linux права доступа только файлы

How to change the default GCC compiler in Ubuntu?

I have installed gcc-3.3/g++-3.3 on ubuntu 11.04 which already has gcc/g++-4.4. So in my system both gcc-3.3 and 4.4 are available. I am able to call both compilers as I want. If I just call the command gcc then gcc-4.4 will get called. To call gcc-3.3, I have to use the command gcc-3.3 . How can I change the default compiler as gcc-3.3? When I execute the command gcc it should call the gcc-3.3 and not gcc-4.4. In addition, how can I change the variable CXX in a make file to gcc-3.3? I wish to change one common global place in the system instead of changing all make files.

For CXX flag you can invoke CXX=gcc-3.3 or export CXX=gcc-3.3 and then make however when you changed it globally with update-alternatives it will already use gcc-3.3 and this is not necessary.

@RoboAlex: updated my answer again to take into account your CXX environment variable request. However, please note that it will only serve in case you modify the update-alternatives later.

You only need to change your PATH. Most of the answers mention the alternatives system, but both the Debian and the LLVM maintainers agree that the alternatives system should be used for alternatives, NOT for versioning. Further explained in my answer.

8 Answers 8

As @Tommy suggested, you should use update-alternatives .
It assigns values to every software of a family, so that it defines the order in which the applications will be called.

It is used to maintain different versions of the same software on a system. In your case, you will be able to use several declinations of gcc , and one will be favoured.

To figure out the current priorities of gcc, type in the command pointed out by @tripleee’s comment:

update-alternatives --query gcc 

Now, note the priority attributed to gcc-4.4 because you’ll need to give a higher one to gcc-3.3 .
To set your alternatives, you should have something like this (assuming your gcc installation is located at /usr/bin/gcc-3.3 , and gcc-4.4 ‘s priority is less than 50):

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-3.3 50 

Finally, you can also use the interactive interface of update-alternatives to easily switch between versions. Type update-alternatives —config gcc to be asked to choose the gcc version you want to use among those installed.

Читайте также:  Linux переключение между приложениями

Now, to fix the CXX environment variable systemwide, you need to put the line indicated by @DipSwitch’s in your .bashrc file (this will apply the change only for your user, which is safer in my opinion):

echo 'export CXX=/usr/bin/gcc-3.3' >> ~/.bashrc 

@thang also called cc : root@host:/root# update-alternatives —get-selections | grep ‘/usr/bin/gcc’ cc auto /usr/bin/gcc

When I run the ‘update-alternatives —config gcc’, nothing happens, even though I have the system’s gcc and anaconda one

how could you do this automatically though? this requires a manual step to check the output of update-alternatives —query gcc , which might not work eg in an automated provisioning script. Also, how can this be made platform agnostic?

Here’s a complete example of jHackTheRipper’s answer for the TL;DR crowd. 🙂 In this case, I wanted to run g++-4.5 on an Ubuntu system that defaults to 4.6. As root :

apt-get install g++-4.5 update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 100 update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.5 50 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 100 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.5 50 update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.6 100 update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.5 50 update-alternatives --set g++ /usr/bin/g++-4.5 update-alternatives --set gcc /usr/bin/gcc-4.5 update-alternatives --set cpp-bin /usr/bin/cpp-4.5 

Here, 4.6 is still the default (aka «auto mode»), but I explicitly switch to 4.5 temporarily (manual mode). To go back to 4.6:

update-alternatives --auto g++ update-alternatives --auto gcc update-alternatives --auto cpp-bin 

(Note the use of cpp-bin instead of just cpp . Ubuntu already has a cpp alternative with a master link of /lib/cpp . Renaming that link would remove the /lib/cpp link, which could break scripts.)

Источник

yunqu / install-gcc.md

First, add the ubuntu-toolchain-r/test PPA to your system with:

sudo apt install software-properties-common sudo add-apt-repository ppa:ubuntu-toolchain-r/test 

Install the desired GCC and G++ versions by typing:

sudo apt install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9 

The commands below will configure alternative for each version and associate a priority with it. The default version is the one with the highest priority, in our case that is gcc-9.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7 

Later if you want to change the default version use the update-alternatives command:

sudo update-alternatives --config gcc 

There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

 Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/gcc-9 90 auto mode 1 /usr/bin/gcc-7 70 manual mode 2 /usr/bin/gcc-8 80 manual mode 3 /usr/bin/gcc-9 90 manual mode 

Press to keep the current choice[*], or type selection number: You will be presented with a list of all installed GCC versions on your Ubuntu system. Enter the number of the version you want to be used as a default and press Enter.

Читайте также:  Починить файловую систему линукс

The command will create symbolic links to the specific versions of GCC and G++.

Источник

How to switch GCC version using update-alternatives

Multiple versions of GCC can be installed and used on Ubuntu as described here. The update-alternatives tool makes it easy to switch between multiple versions of GCC.

On Ubuntu, gcc and g++ are just symbolic links to the actual binaries of a specific version of GCC. By switching the version, invoking gcc will execute the particular version of the compiler binary that you wish. You can make any of these version as the default at any time effortlessly.

As an example, I had installed GCC version 4.8 from the Ubuntu repositories. This was the default version of GCC, so gcc was a symlink to gcc-4.8 binary. Wanting to use some new C++11 features I installed version 4.9 of GCC. This compiler can be invoked using gcc-4.9 . I now want to switch the default gcc to invoke gcc-4.9 . I also want the freedom to switch back 4.8 as the default whenever I want. You can switch the symlinks yourself manually, but using this tool makes it easy and clean.

  • Decide which set of symbolic links you want to group together as one unit. I like to switch /usr/bin/gcc and /usr/bin/g++ together.
  • Pass update-alternatives the first version of these symbolic links. Here I will inform about the 4.8 version of these tools and links:
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 100 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8

Here, we have provided the gcc as the master and g++ as slave. Multiple slaves can be appended along with master. When master symbolic link is changed, the slaves will be changed too.

$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 50 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9 
$ sudo update-alternatives --config gcc

Tried with: Ubuntu 14.04

Источник

Оцените статью
Adblock
detector