How to switch between different versions of g++

I needed to compile an old code and needed to use older versions of g++. Here is how I did it:

  • First, install the older version

sudo apt install g++-4.8 g++-5 g++-6 g++-7

  • Then, tell the system that there is a new alternative

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 1
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 1
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-6 1

  • Finally you can choose which g++ you want to use:

sudo upate-alternatives --config g++

 

Comments