在 Ubuntu 20.04 上安装 gcc/g++ 11,并使用 update-alternatives 管理多个版本。

✅ 一、安装 GCC/G++ 11

Ubuntu 20.04 自带仓库就含 gcc-11,所以直接安装即可:

sudo apt update
sudo apt install gcc-11 g++-11

安装后你可以检查版本:

gcc-11 --version
g++-11 --version

✅ 二、使用 update-alternatives 管理多个 GCC/G++ 版本

假设你当前已有 gcc-9 / g++-9,并且想在两个版本之间切换,这就是推荐方式。

  1. 为 gcc 设置 alternatives
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110

  2. 为 g++ 设置 alternatives
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 110

✅ 三、选择要使用的版本
切换 gcc:
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-11 110 auto mode
    1 /usr/bin/gcc-9 90 manual mode
    2 /usr/bin/gcc-11 110 manual mode

选择 1 或 2 就行。

切换 g++:
sudo update-alternatives --config g++

✅ 四、检查当前版本
gcc --version
g++ --version

posted @ 2025-11-18 19:59  流年中渲染了微笑  阅读(34)  评论(0)    收藏  举报