Gcc多版本安装和切换

添加本地source

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update

安装 gcc9 和 g++9(ubuntu18.04)

sudo apt-get install gcc-9 gcc-9-multilib g++-9 g++-9-multilib

安装 gcc13 和 g++13(ubuntu22.04)

sudo apt install gcc-13 -y
sudo apt install g++-13 -y
sudo apt install gcc-13-multilib -y
sudo apt install g++-13-multilib -y

添加和修改默认配置

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 80
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 80

注:最后那个数字代表优先级,默认选优先级高的,数字越小优先级越高

sudo update-alternatives --config gcc出现下列类似信息

  选择      路径            优先级  状态
------------------------------------------------------------
* 0            /usr/bin/gcc-9  90        自动模式
  1            /usr/bin/gcc-8  50        手动模式
  2            /usr/bin/gcc-9  90        手动模式
  3            /usr/bin/gcc-7  30        手动模式

要维持当前值[*]请按回车键,或者键入选择的编号:

输入需要作为默认编辑器的gcc版本的编号 1-3
gcc

sudo update-alternatives --config gcc

g++

sudo update-alternatives --config g++

删除已有配置,如果想删除可选项的话可以键入以下指令:

sudo update-alternatives --remove gcc /usr/bin/gcc-9

当前版本确认

gcc --version
g++ --version

参考:


➜  /home/mi/local/n62-4.0 sudo update-alternatives --config gcc
There are 4 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-9    50        auto mode
  1            /usr/bin/gcc-10   45        manual mode
  2            /usr/bin/gcc-11   40        manual mode
* 3            /usr/bin/gcc-13   35        manual mode
  4            /usr/bin/gcc-9    50        manual mode

Press <enter> to keep the current choice[*], or type selection number:  
➜  /home/mi/local/n62-4.0 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc 30                                          
➜  /home/mi/local/n62-4.0 sudo update-alternatives --config gcc
➜  /home/mi/local/n62-4.0 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 30 --slave /usr/bin/g++ g++ /usr/bin/g++-11
➜  /home/mi/local/n62-4.0 sudo update-alternatives --config gcc
There are 4 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-9    50        auto mode
  1            /usr/bin/gcc-10   45        manual mode
  2            /usr/bin/gcc-11   30        manual mode
* 3            /usr/bin/gcc-13   35        manual mode
  4            /usr/bin/gcc-9    50        manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/gcc-11 to provide /usr/bin/gcc (gcc) in manual mode
➜  /home/mi/local/n62-4.0 gcc --version
gcc (Ubuntu 11.4.0-2ubuntu1~20.04) 11.4.0
Copyright (C) 2021 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.

➜  /home/mi/local/n62-4.0 g++ --version
g++ (Ubuntu 11.4.0-2ubuntu1~20.04) 11.4.0
Copyright (C) 2021 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.

➜  /home/mi/local/n62-4.0 sudo update-alternatives --config gcc
There are 4 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-9    50        auto mode
  1            /usr/bin/gcc-10   45        manual mode
* 2            /usr/bin/gcc-11   30        manual mode
  3            /usr/bin/gcc-13   35        manual mode
  4            /usr/bin/gcc-9    50        manual mode

Press <enter> to keep the current choice[*], or type selection number: ^C
➜  /home/mi/local/n62-4.0 
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值