距离上次更新本文已经过去了 424 天,文章部分内容可能已经过时,请注意甄别

本文将介绍如何在 Ubuntu22.04 版本下实现 gcc 版本的快速切换。

前言

有的时候,不同版本的 gcc 会造成一些细微的差异,导致相关的一些工具不兼容,比如用于单元测试覆盖率生成的 gcov/lcov 工具,在不同的 gcc 版本下可能会出现不同的结果。

为了确定是否是 gcc 编译器版本不同造成的这些差异,有的时候需要在一台主机上装多个版本的 gcc,不停的切换以测试问题。

假设我们的 ubuntu 主机上安装了 gcc 11.4 版本和 gcc 9.5 版本,且当前 gcc 命令指向的是 11.4 版本,那么你可以使用 gcc-9 命令来使用 9.5 版本的 gcc。

但是,对于一些已经写死使用 gcc 这个命令的脚本或编译工具而言,它们就不是那么好修改 gcc 为 gcc-9 的,我们需要做的就是让系统的 gcc 命令能在 11.4 版本和 9.5 版本之间进行切换

其中一个办法是手动替换 gcc 和 g++ 的命令软连接,但是这个方法并不方便,ubuntu 下有更好的操作,请看下文。

安装 gcc9.5

默认情况下,ubuntu22.04 安装的 gcc 版本是 11.4

plaintext
1
2
3
4
5
> gcc --version          
gcc (Ubuntu 11.4.0-1ubuntu1~22.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.

先给你的主机安装上 gcc9.4 版本

plaintext
1
sudo apt install gcc-9 g++-9

安装之后,你应该可以通过 gcc-9 命令来使用 9.5 版本的 gcc 了

plaintext
1
2
3
4
5
> g++-9 --version
g++-9 (Ubuntu 9.5.0-1ubuntu1~22.04) 9.5.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.

配置 update-alternatives

我们可以将 11 和 9 版本添加到 update-alternatives 工具中,就可以方便的切换,不需要手动设置命令的软连接。

工具基本使用命令

如下是工具使用的一个基本示例

bash
1
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 20 --slave /usr/bin/g++ g++ /usr/bin/g++-11
  1. --install 代表我们需要注册一个新的服务名
  2. /usr/bin/gcc 代表我们目标的最终地址,切换软链接的时候会切换该地址的软链接
  3. gcc 代表我们用于管理服务的名字
  4. /usr/bin/gcc-11 代表被管理的命令的绝对路径(会用这个命令来替换第二个参数的软链接)
  5. 20 代表优先级,数字越大优先级越高。
  6. --slave 代表从属命令,参数顺序和前面这几个是一样的,配置的是 g++ 命令

在 update-alternatives 命令的帮助中可以看到 install 和 slave 的关系,slave 命令就是跟着 install 命令来使用的

plaintext
1
2
3
--install <link> <name> <path> <priority>
[--slave <link> <name> <path>] ...
add a group of alternatives to the system.

它是什么意思呢?其实就是让主从命令有一个对应关系:当我们把 gcc 命令切换成 11 版本后,作为 slave 的 g++ 命令也会跟着一起变化。

slave 可以添加多个,后文会做演示。

添加 gcc 版本配置

使用如下命令查看当前可以用来直接替换 gcc 的版本号,会发现么有,因为两个 gcc 版本都没有加入到这个工具中。

plaintext
1
2
> sudo update-alternatives --config gcc
update-alternatives: error: no alternatives for gcc

使用如下命令查看一下当前装好的 gcc 包,可以看到有 gcc 11 和 9。

plaintext
1
2
3
4
5
6
7
8
9
10
11
> dpkg -l | grep gcc
ii gcc 4:11.2.0-1ubuntu1 amd64 GNU C compiler
ii gcc-11 11.4.0-1ubuntu1~22.04 amd64 GNU C compiler
ii gcc-11-base:amd64 11.4.0-1ubuntu1~22.04 amd64 GCC, the GNU Compiler Collection (base package)
ii gcc-12-base:amd64 12.3.0-1ubuntu1~22.04 amd64 GCC, the GNU Compiler Collection (base package)
ii gcc-9 9.5.0-1ubuntu1~22.04 amd64 GNU C compiler
ii gcc-9-base:amd64 9.5.0-1ubuntu1~22.04 amd64 GCC, the GNU Compiler Collection (base package)
ii lib32gcc-s1 12.3.0-1ubuntu1~22.04 amd64 GCC support library (32 bit Version)
ii libgcc-11-dev:amd64 11.4.0-1ubuntu1~22.04 amd64 GCC support library (development files)
ii libgcc-9-dev:amd64 9.5.0-1ubuntu1~22.04 amd64 GCC support library (development files)
ii libgcc-s1:amd64 12.3.0-1ubuntu1~22.04 amd64 GCC support library

执行如下命令将 gcc-9 添加到 update-alternatives 工具中,让我们可以通过 config 来切换 gcc 版本

plaintext
1
2
> sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 20 --slave /usr/bin/g++ g++ /usr/bin/g++-9
update-alternatives: using /usr/bin/gcc-9 to provide /usr/bin/gcc (gcc) in auto mode

使用如下命令查看可切换的 gcc 版本,因为当前只给工具里添加了一个 gcc-9 版本,所以会显示 only one alternative,即只有一个可选项的意思,工具会认为没有啥好配置的。

plaintext
1
2
3
> sudo update-alternatives --config gcc
There is only one alternative in link group gcc (providing /usr/bin/gcc): /usr/bin/gcc-9
Nothing to configure.

这时候我们可以把原本安装好的 11.4 版本的 gcc 也给添加到这个工具里面。优先级的数字可以根据你的情况自行调整

plaintext
1
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 25 --slave /usr/bin/g++ g++ /usr/bin/g++-11

切换 gcc 版本成功

然后再次执行 sudo update-alternatives --config gcc,此时就能看到两个版本的可选项了。

plaintext
1
2
3
4
5
6
7
8
9
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-9 20 auto mode
1 /usr/bin/gcc-11 20 manual mode
2 /usr/bin/gcc-9 20 manual mode

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

键入序号来选择你需要切换的 gcc 版本,回车即可

plaintext
1
2
3
4
5
> gcc --version
gcc (Ubuntu 9.5.0-1ubuntu1~22.04) 9.5.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.

可以看到,当前的系统 gcc 已经被换成了 9.5 版本的了,切换成功!而且因为我们通过 --slave 配置了 g++ 命令的替换操作,所以 g++ 的版本也跟着一起替换成 9.5 了

plaintext
1
2
3
4
5
> g++ --version
g++ (Ubuntu 9.5.0-1ubuntu1~22.04) 9.5.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.

将 gcc 换回 gcc 11 版本是相同的操作,这里不再赘述。

添加多个 slave

和 gcc 捆绑的还有一个用于分支覆盖率检测的 gcov 命令,我们同样可以通过 --slave 选项将其绑定到版本中,让 gcov 的命令版本也会更着 gcc 版本一同切换

plaintext
1
2
3
4
> sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 25 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11
update-alternatives: updating alternative /usr/bin/gcc-11 because link group gcc has changed slave links

> sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 20 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9

此时使用 sudo update-alternatives --config gcc 切换 gcc 版本后,gcov 命令的版本也会跟着变化。这样我们能保证 gcov 和 gcc 版本是一致的,不会出现不一致导致的兼容性问题。

plaintext
1
2
3
4
5
6
> gcov --version
gcov (Ubuntu 9.5.0-1ubuntu1~22.04) 9.5.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.

从工具中删除 gcc 版本

如果需要从这个工具中删除某个版本,执行如下命令

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

删除后就无法通过该工具切换 gcc 版本为 9 了。