在CentOS8系统中,默认gcc版本已经是8.x.x版本,但是在一些场景中,还是需要高版本的gcc,网上一些作死的文章还在复制粘贴的告诉你如何编译升级gcc版本。
之前吴昊也写过CentOS完美升级gcc版本方法:https://blog.whsir.com/post-4975.html,但是之前的文章不适用CentOS8的版本,吴昊也强调过,不要去编译升级gcc,编译升级后可能会给你带来额外的麻烦。
本文基于CentOS8.3版本验证通过,仅适用于CentOS8、Redhat8版本
在CentOS8版本中,红帽也提供了开发工具包管理gcc版本,install的包名从devtoolset-x变成了gcc-toolset-x,通过该方式升级gcc好处就是随时可以切换gcc版本,不破坏原有gcc环境。
截至本文发稿前gcc-toolset仅支持gcc9、gcc10、gcc11版本
1 2 3 |
gcc-toolset-9对应gcc9.x.x版本 gcc-toolset-10对应gcc10.x.x版本 gcc-toolset-11对应gcc11.x.x版本 |
例:升级gcc至10.x.x版本
配置yum源
1 |
vi /etc/yum.repos.d/Centos-8.repo |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
[base] name=CentOS-$releasever - Base - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/BaseOS/$basearch/os/ gpgcheck=1 enabled=1 [extras] name=CentOS-$releasever - Extras - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os/ gpgcheck=1 enabled=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official [AppStream] name=CentOS-$releasever - AppStream - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/AppStream/$basearch/os/ gpgcheck=1 enabled=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official [8-AppStream] name=CentOS-$releasever - 8-AppStream baseurl=http://mirrors.aliyun.com/centos/8-stream/AppStream/$basearch/os/ gpgcheck=0 enabled=1 |
安装gcc-toolset-10
1 |
dnf install gcc-toolset-10 |
激活gcc版本,使其生效
1 |
scl enable gcc-toolset-10 bash |
或
1 |
source /opt/rh/gcc-toolset-10/enable |
此时通过gcc --version命令可以看到,gcc版本已经变成10.x.x,值得注意的是这仅仅在当前bash生效,如果需要永久生效,可以请自行添加环境变量。
原文链接:CentOS8完美升级gcc版本方法,转载请注明来源!