GCC 多版本切換

冰雪封存的記憶發表於2020-01-17
ls -l /usr/bin/gcc*
root@c98c1b416c01:/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
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
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

版本號 gcc-5、gcc-4.4、g++-5 、g++-4.4 以及 優先順序數值 40、50 可以根據自己需要更改。優先順序數值範圍為0到100。設定好 GCC、G++ 各版本優先順序後可進行優先順序檢視,以及個版本切換。

sudo update-alternatives --config gcc
root@c98c1b416c01:/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: 
sudo update-alternatives --config g++
root@c98c1b416c01:/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: 

另一種切換方法

在 ~/.bashrc 檔案中增新增

alias gcc='/usr/bin/gcc-4.4'
alias g++='/usr/bin/g++-4.4'
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章