多版本GCC安装及切换

1 背景

最近在研究C++20中的协程需要安装GCC版本10。用到GCC多版本切换,记录步骤。

2 安装

2.1 Ubuntu 20.04

运行如下命令安装两个版本编译器:

sudo apt install gcc-9 g++-9
sudo apt install gcc-10 g++-10

2.2 Ubuntu 18.04

默认安装最高版本gcc-7,添加Toolchain Test Builds PPA(个人软件包存档),它提供了GCC 10的最新版本

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt install gcc-9 g++-9
sudo apt install gcc-10 g++-10

3 配置

使用update-alternatives命令配置多编译器版本:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10

说明:

  • gcc与g++版本绑定在一起了,修改gcc版本会同时修改g++版本

4 切换

使用如下命令切换编译器版本:

sudo update-alternatives --config gcc

4.1 切换到版本9

$ 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-10   100       auto mode
  1            /usr/bin/gcc-10   100       manual mode
  2            /usr/bin/gcc-9    90        manual mode

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

查看版本:

$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.3) 9.4.0
Copyright (C) 2019 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++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.3) 9.4.0
Copyright (C) 2019 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.

4.2 切换到版本10

$ 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-10   100       auto mode
  1            /usr/bin/gcc-10   100       manual mode
  2            /usr/bin/gcc-9    90        manual mode

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

查看版本:

$ gcc --version
gcc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
Copyright (C) 2020 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++ --version
g++ (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
Copyright (C) 2020 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.
### 实现多个GCC版本间的切换 在Ubuntu操作系统中,为了满足不同的开发需求,可以安装并管理多个版本GCC编译器。这不仅有助于解决因特定项目依赖于某个GCC版本而产生的兼容性问题,还能够方便开发者测试代码在不同环境下的表现。 #### 安装所需的不同版本GCC编译器 要实现在同一台机器上拥有多种GCC版本的能力,首先需要确保这些版本已经被正确安装。对于希望使用的每一个GCC版本,都需要执行对应的安装指令: ```bash sudo apt update && sudo apt install gcc-<version> g++-<version> ``` 这里的`<version>`应替换为实际想要安装的具体版本号,比如`gcc-10`, `g++-10`用于安装GCC 10及其配套工具链[^1]。 #### 使用update-alternatives配置默认GCC版本 一旦成功安装了所需的各个GCC版本,则可通过Linux系统的`update-alternatives`机制轻松设置当前工作环境中所采用的默认GCC/G++版本。此过程涉及向系统注册新选项以及设定优先级等操作: ```bash sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-<version> <priority> sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-<version> <priority> ``` 其中,`<version>`代表已安装的目标GCC版本;`<priority>`是一个整数值,用来指示当存在多个候选者时哪个应该被选作默认项——通常情况下较高的数字意味着更高的优先权。例如,如果打算让GCC 10成为新的首选编译器,可将其关联命令赋予较大的权重如100[^3]。 #### 切换至指定版本作为全局默认值 完成了上述准备工作之后,便能利用简单的交互界面快速更改正在生效的那个GCC版本: ```bash sudo update-alternatives --config gcc sudo update-alternatives --config g++ ``` 运行以上任一条命令后会弹出一个列表供用户从中挑选期望激活的那一版GCC或G++。按照提示输入对应编号即可完成转换动作。 #### 验证当前使用的GCC版本 最后一步是确认变更是否已经生效。只需简单调用如下命令查看输出结果中的版本信息部分: ```bash gcc --version g++ --version ``` 若一切顺利的话,终端应当返回先前选定的新版本的相关详情。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysnow010

你的鼓励就是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值