在 CentOS 上切换 GCC 版本,常见有 三种方式,从简单到灵活依次如下:
scl(推荐,适合 CentOS 7/8)CentOS 官方提供 Software Collections(SCL),可安装多个 GCC 版本且不破坏系统默认。
yum install centos-release-scl -y
yum install devtoolset-9-gcc devtoolset-9-gcc-c++ -y
常见版本:
scl enable devtoolset-9 bash
gcc --version
echo "source /opt/rh/devtoolset-9/enable" >> ~/.bashrc
source ~/.bashrc
alternatives(系统级切换)适合你 自己编译或安装了多个 gcc 的情况。
alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8.5 50
alternatives --config gcc
alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90
alternatives --config g++
export CC=/opt/gcc-9/bin/gcc
export CXX=/opt/gcc-9/bin/g++
适合编译软件时临时使用。
gcc --version
which gcc
| 场景 | 推荐方式 |
|---|---|
| CentOS 7/8 普通用户 | scl |
| 多版本系统级管理 | alternatives |
| 编译某个项目临时用 | 手动指定路径 |
如果你告诉我 CentOS 版本 + 想用的 GCC 版本,我可以给你精确的命令。