温馨提示×

centos如何降低gcc版本

小樊
45
2026-09-14 20:53:42
栏目: 智能运维

在 CentOS 上“降低 GCC 版本”通常有以下几种常见做法,按推荐程度从高到低说明。


一、使用 devtoolset(最推荐,CentOS 7/8 都适用)

CentOS 官方通过 Software Collections (SCL) 提供了多个 GCC 版本,不会破坏系统自带 GCC。

1️⃣ 安装 SCL 源

# CentOS 7
yum install -y centos-release-scl

# CentOS 8(已停止维护,可用 Stream 或手动源)
dnf install -y centos-release-stream

2️⃣ 查看可安装的 gcc 版本

yum list available devtoolset-*

常见版本:

  • devtoolset-7(GCC 7)
  • devtoolset-8(GCC 8)
  • devtoolset-9(GCC 9)
  • devtoolset-10(GCC 10)

3️⃣ 安装旧版本 GCC

例如安装 GCC 7:

yum install -y devtoolset-7-gcc devtoolset-7-gcc-c++

4️⃣ 临时使用

scl enable devtoolset-7 bash
gcc --version

5️⃣ 永久生效(推荐写法)

echo "source /opt/rh/devtoolset-7/enable" >> ~/.bashrc

优点

  • 不影响系统 GCC
  • 可多版本共存
  • 安全可靠

二、使用 alternatives 切换已安装的 GCC(适合已装多个版本)

如果你已经通过源码或 SCL 装了多个 gcc:

alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70
alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90

# 切换
alternatives --config gcc

⚠️ 注意

  • 不能“自动降级”
  • 必须已经存在多个 gcc 可执行文件

三、源码编译安装旧版本 GCC(不推荐,麻烦)

适合必须特定版本极老系统

# 示例:安装 GCC 5.5
wget https://mirrors.tuna.tsinghua.edu.cn/gnu/gcc/gcc-5.5.0/gcc-5.5.0.tar.gz
tar -xf gcc-5.5.0.tar.gz
cd gcc-5.5.0
./contrib/download_prerequisites
mkdir build && cd build
../configure --prefix=/usr/local/gcc-5.5 --enable-languages=c,c++
make -j$(nproc)
make install

然后再用 alternatives 切换。

❌ 缺点

  • 编译时间长
  • 依赖复杂
  • 容易破坏系统

四、CentOS 版本与默认 GCC 对照(参考)

CentOS 版本 默认 GCC
CentOS 7 4.8.5
CentOS 8 8.4+
CentOS Stream 9/10+

✅ 总结建议

场景 推荐方式
想用旧 GCC 编译项目 devtoolset
多版本切换 devtoolset + alternatives
极特殊需求 源码编译

如果你能告诉我:

  • CentOS 版本(cat /etc/redhat-release
  • 当前 GCC 版本
  • 想降到哪个版本

我可以给你精确命令

0