GCC multi version switch

Time:2021-6-9

View the installed GCC version of the system

ls -l /usr/bin/gcc*
[email protected]:/home# ls -l /usr/bin/gcc*
lrwxrwxrwx 1 root root     21 Jan 16 22:09 /usr/bin/gcc -> /etc/alternatives/gcc
-rwxr-xr-x 1 root root 255080 Mar 20  2014 /usr/bin/gcc-4.4
-rwxr-xr-x 1 root root 915736 Oct  4 19:23 /usr/bin/gcc-5
lrwxrwxrwx 1 root root      8 Feb 11  2016 /usr/bin/gcc-ar -> gcc-ar-5
-rwxr-xr-x 1 root root  31136 Oct  4 19:23 /usr/bin/gcc-ar-5
lrwxrwxrwx 1 root root      8 Feb 11  2016 /usr/bin/gcc-nm -> gcc-nm-5
-rwxr-xr-x 1 root root  31136 Oct  4 19:23 /usr/bin/gcc-nm-5
lrwxrwxrwx 1 root root     12 Feb 11  2016 /usr/bin/gcc-ranlib -> gcc-ranlib-5
-rwxr-xr-x 1 root root  31136 Oct  4 19:23 /usr/bin/gcc-ranlib-5
-rwxr-xr-x 1 root root   2189 Dec  3  2015 /usr/bin/gccmakedep

Setting the priority of GCC versions

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 60
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50

Setting the priority of G + + versions

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 60
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 50

Version numbers gcc-5, gcc-4.4, G + + – 5, G + + – 4.4 and priority values 40 and 50 can be changed according to your own needs. Priority values range from 0 to 100. After setting the priority of GCC and G + + versions, you can view the priority and switch versions.

GCC version switching

sudo update-alternatives --config gcc
[email protected]:/home# update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

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

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

G + + version switching

sudo update-alternatives --config g++
[email protected]:/home# update-alternatives --config g++
There are 2 choices for the alternative g++ (providing /usr/bin/g++).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/g++-4.4   60        auto mode
  1            /usr/bin/g++-4.4   60        manual mode
  2            /usr/bin/g++-5     50        manual mode

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

Another switching method

Add in ~ /. Bashrc file

alias gcc='/usr/bin/gcc-4.4'
alias g++='/usr/bin/g++-4.4'

This work adoptsCC agreementReprint must indicate the author and the link of this article