无意中看到altenative这个选项,然后就查询了一下.了解了用法.
首先安装多版本的gcc/g++之后,
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 100
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 50
这里的100,50指的赋给这两个不同版本的gcc的优先级,值越大则优先级越高. 可以在/usr/bin目录下看到多个gcc-<version>,然后就可以使用
sudo update-alternative config gcc
然后就可以看到
(base) ➜ ~ sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-7 100 auto mode
1 /usr/bin/gcc-7 100 manual mode
2 /usr/bin/gcc-8 8 manual mode
* 3 /usr/bin/gcc-9 50 manual mode
Press <enter> to keep the current choice[*], or type selection number:
其中gcc-8是系统自带的版本, 另外两个是新安装的. 按照说明选择需要的版本就完成配置了.
(base) ➜ ~ sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-7 100 auto mode
1 /usr/bin/gcc-7 100 manual mode
2 /usr/bin/gcc-8 8 manual mode
* 3 /usr/bin/gcc-9 50 manual mode
Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/bin/gcc-7 to provide /usr/bin/gcc (gcc) in auto mode
现在系统的gcc版本就是gcc-7了
(base) ➜ ~ gcc --version
gcc (Ubuntu 7.5.0-6ubuntu2) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++也是同理的操作.