Friday, May 15, 2015

Install multiple versions of gcc at the same time

Objective


Install gcc/g++ version 4.9 and 5.1 on my current Linux Mint 17.1 Rebecca distribution. Use gcc 4.9 to compile the VirtualBox kernel driver.

Motivation


Recently I upgraded my gcc to version 5.1 via the Synaptic package manager as an suggested update. By doing this, my already installed version of gcc version 4.9 got lost. Few days later I updated my VirtualBox 4.3.26 to version 4.3.28. As a consequence of the update, all VirtualBox guest-systems refused to start, by claiming that I have update also the kernel drivers for virtualbox. Ok, no problem, as it gave me the command to do so ... but wait, I am running on an yet not officially supported kernel (as I explained in a previous post). The command updating or let's say compiling the kernel driver for VirtualBox miserably failed, due to a missing header file in gcc 5.1.

Prerequisites


  • Linux Mint 17.1 Rebecca
  • Kernel version 3.17.1
  • VirtualBox 4.3.28

Problem


The command to update the kernel driver for VirtualBox 4.3.26 was:

$> sudo /etc/init.d/vboxdrv setup

I still just had gcc 5.1 installed and unfortunatelly got the following error:

Stopping VirtualBox kernel modules ...done.
Uninstalling old VirtualBox DKMS kernel modules ...done.
Trying to register the VirtualBox kernel modules using DKMSError! Bad return status for module build on kernel: 3.17.1-031701-generic (x86_64)
Consult /var/lib/dkms/vboxhost/4.3.28/build/make.log for more information.
...failed!
(Failed, trying without DKMS)
Recompiling VirtualBox kernel modules ...failed!
(Look at /var/log/vbox-install.log to find out what went wrong)

The error in the log "/var/lib/dkms/vboxhost/4.3.28/build/make.log" mentioned above was ...

LD /var/lib/dkms/vboxhost/4.3.28/build/built-in.o
LD /var/lib/dkms/vboxhost/4.3.28/build/vboxdrv/built-in.o
CC [M] /var/lib/dkms/vboxhost/4.3.28/build/vboxdrv/linux/SUPDrv-linux.o
In file included from include/linux/compiler.h:54:0,
from /var/lib/dkms/vboxhost/4.3.28/build/vboxdrv/include/iprt/types.h:116,
from /var/lib/dkms/vboxhost/4.3.28/build/vboxdrv/include/VBox/types.h:30,
from /var/lib/dkms/vboxhost/4.3.28/build/vboxdrv/linux/../SUPDrvInternal.h:35,
from /var/lib/dkms/vboxhost/4.3.28/build/vboxdrv/linux/SUPDrv-linux.c:31:
include/linux/compiler-gcc.h:106:30: fatal error: linux/compiler-gcc5.h: No such file or directory
compilation terminated.
make[2]: *** [/var/lib/dkms/vboxhost/4.3.28/build/vboxdrv/linux/SUPDrv-linux.o] Error 1
make[1]: *** [/var/lib/dkms/vboxhost/4.3.28/build/vboxdrv] Error 2
make: *** [_module_/var/lib/dkms/vboxhost/4.3.28/build] Error 2

Solution


Install multiple versions of gcc


There is a very convenient package called update-alternatives to do the job of installing multiple versions of gcc and select the proper version for your current compile tasks.

To install just gcc/g++ 4.9 and gcc/g++ 5.1, first get rid of any other version by cleaning up the system:

$> sudo update-alternatives --remove-all gcc && sudo update-alternatives --remove-all g++

After the command returned you can install your required versions of gcc/g++:

$> sudo apt-get install gcc-4.9 gcc-5 g++-4.9 g++-5

Be patient because this may take a while if your don't have the packages allready installed.

If you get an error message like:

Paketlisten werden gelesen... Fertig
Abhängigkeitsbaum wird aufgebaut.
Statusinformationen werden eingelesen.... Fertig
Note, selecting 'gcc-4.9-base' for regex 'gcc-4.9'
E: Paket gcc-5 kann nicht gefunden werden.
E: Paket g++-4.9 kann nicht gefunden werden.
E: Mittels regulärem Ausdruck »g++-4.9« konnte kein Paket gefunden werden.
E: Paket g++-5 kann nicht gefunden werden.
E: Mittels regulärem Ausdruck »g++-5« konnte kein Paket gefunden werden.

You have to add a new repository "ppa:ubuntu-toolchain-r/test"
source first by exexuting:

$> sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$> sudo apt-get update

Install the gcc versions in update-alternatives


To make update-alternatives aware of your installed compilers you have to execute the following additional installer commands:

$> sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 10
$> sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 20

$> sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 10
$> sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 20

$> sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
$> sudo update-alternatives --set cc /usr/bin/gcc

$> sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
$> sudo update-alternatives --set c++ /usr/bin/g++

Configure update-alternatives


As a last step you have to configure the default commands for gcc, g++:
$> sudo update-alternatives --config gcc

If everything went well, you should be able to interactively choose which C-Compiler version you want to activate:

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

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

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


The same can be done for the C++-Compiler g++:

$> sudo update-alternatives --config g++

I've choosen gcc-4.9 and g++-4.9 for the moment.

Recompile the kernel module for VirtualBox with gcc 4.9


Finally, I typed again:

$> sudo /etc/init.d/vboxdrv setup

... and this time the update was successful:

Stopping VirtualBox kernel modules ...done.
Uninstalling old VirtualBox DKMS kernel modules ...done.
Trying to register the VirtualBox kernel modules using DKMS
...done.
Starting VirtualBox kernel modules ...done.
cschmidt@pippin:~$
cschmidt@pippin:~$ sudo /etc/init.d/vboxdrv setup
Stopping VirtualBox kernel modules ...done.
Uninstalling old VirtualBox DKMS kernel modules ...done.
Trying to register the VirtualBox kernel modules using DKMS ...done.
Starting VirtualBox kernel modules ...done.

As a last step I switched back to gcc/g++ 5.1 by the commands given above.

Have fun!

1 comment: