如何在 Ubuntu 20.04 LTS Focal Fossa 上的多个 GCC 和 G++ 编译器版本之间切换
在本教程中,我们将使用 apt install 命令安装多个版本的 GCC 和 G++ 编译器。此外,通过使用 update-alternatives 工具,您将了解如何轻松地在多个 GCC 和 G++ 编译器版本之间切换以及如何检查当前选择的编译器版本。
在本教程中您将学习:
如何安装多个 GCC 和 G++ 编译器版本
如何创建替代编译器版本列表
如何在多个编译器版本之间切换
在 Ubuntu 20.04 上安装 GCC C 编译器分步说明
安装多个 C 和 C++ 编译器版本:
$ sudo apt install build-essential $ sudo apt -y install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9
使用 update-alternatives 工具创建多个 GCC 和 G++ 编译器替代方案的列表:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 7 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 7 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 8 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9
检查 Ubuntu 20.04 系统上可用的 C 和 C++ 编译器列表,并通过输入相关的选择编号来选择所需的版本:
$ 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-9 9 auto mode 1 /usr/bin/gcc-7 7 manual mode * 2 /usr/bin/gcc-8 8 manual mode 3 /usr/bin/gcc-9 9 manual mode Press
to keep the current choice[*], or type selection number: 对于 C++ 编译器执行:
$ sudo update-alternatives --config g++ There are 3 choices for the alternative g++ (providing /usr/bin/g++). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/g++-9 9 auto mode 1 /usr/bin/g++-7 7 manual mode 2 /usr/bin/g++-8 8 manual mode 3 /usr/bin/g++-9 9 manual mode Press
to keep the current choice[*], or type selection number: 每次切换后检查您当前选择的编译器版本:
$ gcc --version $ g++ --version