ubuntu设置默认gcc版本
想看精简版的直接看大佬的这篇,本文在这篇命令基础上贴了每一步的操作,可和我的截图对比步骤: blog.csdn.net/uniqueyyc/a…
先查看下update-alternatives config:发现暂时未添加任何gcc软链接
test@test-Vostro-3268:~$ sudo update-alternatives --config gcc
update-alternatives: error: no alternatives for gcc
查看已安装的gcc版本:4.7和9
test@test-Vostro-3268:~$ dpkg -l | grep gcc
ii gcc 4:9.3.0-1ubuntu2 amd64 GNU C compiler
ii gcc-10-base:amd64 10.2.0-5ubuntu1~20.04 amd64 GCC, the GNU Compiler Collection (base package)
ii gcc-10-base:i386 10.2.0-5ubuntu1~20.04 i386 GCC, the GNU Compiler Collection (base package)
ii gcc-4.7 4.7.3-12ubuntu1 amd64 GNU C compiler
ii gcc-4.7-base:amd64 4.7.3-12ubuntu1 amd64 GCC, the GNU Compiler Collection (base package)
ii gcc-9 9.3.0-17ubuntu1~20.04 amd64 GNU C compiler
ii gcc-9-base:amd64 9.3.0-17ubuntu1~20.04 amd64 GCC, the GNU Compiler Collection (base package)
ii gcc-9-multilib 9.3.0-17ubuntu1~20.04 amd64 GNU C compiler (multilib support)
ii ... ... ... ...
test@test-Vostro-3268:~$
将4.7添加软链,会提示你当前只有一个gcc选项。
test@test-Vostro-3268:~$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 30 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7
update-alternatives: using /usr/bin/gcc-4.7 to provide /usr/bin/gcc (gcc) in auto mode
test@test-Vostro-3268:~$ sudo update-alternatives --config gcc
There is only one alternative in link group gcc (providing /usr/bin/gcc): /usr/bin/gcc-4.7
Nothing to configure.
将9也添加软链。如果–install指令里的数越大,则优先级越高。4.7的级别30大于9的级别20,故而查看config可见当前默认使用gcc-4.7。
test@test-Vostro-3268:~$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 20 --slave /usr/bin/g++ g++ /usr/bin/g++-9
test@test-Vostro-3268:~$ sudo 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.7 30 auto mode
1 /usr/bin/gcc-4.7 30 manual mode
2 /usr/bin/gcc-9 20 manual mode
Press <enter> to keep the current choice[*], or type selection number: 1
如果在config里,输入选项2,则会将系统默认调用的gcc版本定为9。故而有这两种设置gcc调用优先级的方法。
test@test-Vostro-3268:~$ sudo 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.7 30 auto mode
* 1 /usr/bin/gcc-4.7 30 manual mode
2 /usr/bin/gcc-9 20 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/gcc-9 to provide /usr/bin/gcc (gcc) in manual mode
test@test-Vostro-3268:~$ sudo 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.7 30 auto mode
1 /usr/bin/gcc-4.7 30 manual mode
* 2 /usr/bin/gcc-9 20 manual mode
Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/gcc-4.7 to provide /usr/bin/gcc (gcc) in manual mode
test@test-Vostro-3268:~$
删除软链接:
test@test-Vostro-3268:~$ sudo update-alternatives --remove gcc /usr/bin/gcc-9
test@test-Vostro-3268:~$ sudo update-alternatives --config gcc
There is 1 choice for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-4.7 30 auto mode
* 1 /usr/bin/gcc-4.7 30 manual mode
Press <enter> to keep the current choice[*], or type selection number: 0