温馨提示×

debian怎样切换gcc版本

小樊
44
2026-09-17 00:20:24
栏目: 智能运维

在 Debian 中切换 gcc 版本,通常有以下几种常用方法,最推荐的是 update-alternatives 方式。


一、使用 update-alternatives(推荐)

1. 安装多个 gcc 版本

sudo apt update
sudo apt install gcc-9 gcc-10 gcc-11

2. 将 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-10 100
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110

3. 切换 gcc 版本

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

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

选择对应数字即可。

4. 验证

gcc --version

二、同时切换 g++(很重要)

如果你用 C++,也要切换 g++:

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 110

sudo update-alternatives --config g++

三、临时使用某个版本(不全局切换)

gcc-9 test.c -o test

CC=gcc-10 make

四、查看已安装的 gcc 版本

ls /usr/bin/gcc*
dpkg -l | grep gcc

五、常见注意事项

  • Debian 稳定版可能默认 gcc 版本较老
  • 某些软件(如内核)对 gcc 版本敏感
  • 切换后建议重新生成编译缓存(如 make clean

如果你告诉我 Debian 版本(如 11 / 12)想用的 gcc 版本,我可以给你更精确的命令。

0