Tuesday, June 2, 2015

How to use multiple versions of GCC/G++

In programming life we may require to install and use multiple versions of the compiler on the same system. For an example, to support for C++11 features gcc/g++ v4.7 or later version is requried and for legacy codes which do not require new features, gcc/g++ v4.6 or below is required.

I'm using Ubuntu 14.04.2 LTS and gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2 is the default compiler and the importent thing is it only includes the 'C' compiler, not the 'C++' compiler. As the first step I installed build-essential package using following command,
$ sudo apt-get install build-essential
This gave the expected C++ compiler.

I installed the gcc v4.6 and g++ v4.6 to use as my legacy code compiler using the following command
$ sudo apt-get install gcc-4.6 g++-4.6
Once succesfully installed them, system contains two different versions of C/C++ compilers. Create symbolic links by using update-alternatives keep the record of 2 C/C++ compiler as follows.

$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 10 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
From this point on-words, When required to switch compilers between v4.6 and v4.8 following command can be used
$ sudo update-alternatives --config gcc