CentOS完美升级gcc版本方法

Published on with 0 views and 0 comments

在某些应用场景中,需要特定的gcc版本支持,但是轻易不要去编译gcc、不要去编译gcc、不要去编译gcc,我这里推荐使用红帽提供的开发工具包来管理gcc版本,这样做的好处是随时切换版本,并且可以并存多个版本,不破坏原有gcc环境。

本文基于CentOS7验证通过,不适用于CentOS8版本,CentOS8升级gcc见:https://blog.whsir.com/post-6114.html

红帽官方Developer Toolset文档地址:https://access.redhat.com/documentation/en-us/red_hat_developer_toolset/8/

devtoolset对应gcc的版本

1devtoolset-3对应gcc4****.x.****x版本
2devtoolset-4对应gcc5****.x.****x版本
3devtoolset-6对应gcc6****.x.****x版本
4devtoolset-7对应gcc7****.x.****x版本
5devtoolset-8对应gcc8****.x.****x版本
6devtoolset-9对应gcc9****.x.****x版本
7devtoolset-10对应gcc10****.x.****x版本
8devtoolset-11对应gcc11****.x.****x版本

可通过centos-release-scl源安装devtoolset包

yum install centos-release-scl
yum install devtoolset-11

自行添加源来安装(我这里以CentOS7为例)

vi /etc/yum.repos.d/CentOS-SCLo-scl.repo
[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
baseurl=http://mirror.centos.org/centos/7/sclo/$basearch/rh/
#mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-sclo
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

激活gcc版本,使其生效

scl enable devtoolset-11 bash

source /opt/rh/devtoolset-11/enable

此时通过gcc --version命令可以看到,gcc版本已经变成8.x.x,值得注意的是这仅仅在当前bash生效,如果需要永久生效,可以请自行添加环境变量。

直接替换旧的gcc

旧的gcc是运行的 /usr/bin/gcc,所以将该目录下的gcc/g++替换为刚安装的新版本gcc软连接,免得每次enable

mv /usr/bin/gcc /usr/bin/gcc-4.8.5
ln -s /opt/rh/devtoolset-11/root/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++-4.8.5
ln -s /opt/rh/devtoolset-11/root/bin/g++ /usr/bin/g++

标题:CentOS完美升级gcc版本方法
作者:woyehua
地址:https://blog.stormbirds.cn/articles/2022/03/25/1648142279481.html