Centos GCC 版本升级

Centos GCC 版本升级

在阿里云镜像下的这个目录,不同版本略有差异,一般是在该版本镜像下的sclo/x86_64/rh/ 有红帽封装完成的镜像包。20220921我看的时候,现在gcc 最新的版本是centos 7.9的版本。也就是devtoolset-11,gcc版本为11.2.1。

在这里插入图片描述

https://mirrors.aliyun.com/centos-vault/7.5.1804/sclo/x86_64/rh/
https://mirrors.aliyun.com/centos/7.9.2009/sclo/x86_64/rh/Packages/d/

CentOS 7官方源的gcc最新版本是4.8.5,发布于2015年,年代久远且不支持c++14。要编译c++14及以上项目,必须要升级现有版本或者安装高版本的gcc。红帽其实已经编译好了高版本的gcc,但未更新到base和epel这两个常用源中,而是将这些版本放在scl中。解决办法有两种:手动编译(也可以从其他机器拷贝或者网上下载),或从源安装。

我这边是从源进行安装的

[root@Centos7 ~]# hostnamectl 
   Static hostname: Centos7
         Icon name: computer-vm
           Chassis: vm
        Machine ID: b7efed295d184dd1be0b2d79a9212bd2
           Boot ID: 7abb88ee9ea3443aba1fa784dc9aab49
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-862.el7.x86_64
      Architecture: x86-64
[root@Centos7 ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@Centos7 ~]# 
[root@Centos7 ~]# yum install -y centos-release-scl
...中间忽略

已安装:
  centos-release-scl.noarch 0:2-3.el7.centos                                             
作为依赖被安装:
  centos-release-scl-rh.noarch 0:2-3.el7.centos
完毕!

[root@Centos7 ~]# yum repolist 
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * centos-sclo-rh: mirrors.aliyun.com
 * centos-sclo-sclo: mirrors.ustc.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.aliyun.com
源标识                                            源名称                                          状态
base/7/x86_64                                     CentOS-7 - Base                                 10,072
centos-sclo-rh/x86_64                             CentOS-7 - SCLo rh                               8,112
centos-sclo-sclo/x86_64                           CentOS-7 - SCLo sclo                               816
extras/7/x86_64                                   CentOS-7 - Extras                                  516
updates/7/x86_64                                  CentOS-7 - Updates                               4,160
repolist: 23,676

##列出GCC版本
[root@Centos7 ~]# yum list |grep devtoolset |grep gcc.x86_64
devtoolset-10-gcc.x86_64                   10.2.1-11.2.el7        centos-sclo-rh
devtoolset-11-annobin-plugin-gcc.x86_64    10.38-1.el7            centos-sclo-rh
devtoolset-11-gcc.x86_64                   11.2.1-9.el7           centos-sclo-rh
devtoolset-7-gcc.x86_64                    7.3.1-5.16.el7         centos-sclo-rh
devtoolset-8-gcc.x86_64                    8.3.1-3.2.el7          centos-sclo-rh
devtoolset-9-gcc.x86_64                    9.3.1-2.2.el7          centos-sclo-rh
        
[root@Centos7 ~]# yum --disablerepo="*" --enablerepo="centos-sclo-rh" list available  |grep gcc.x86_64
devtoolset-10-gcc.x86_64                        10.2.1-11.2.el7   centos-sclo-rh
devtoolset-11-annobin-plugin-gcc.x86_64         10.38-1.el7       centos-sclo-rh
devtoolset-11-gcc.x86_64                        11.2.1-9.el7      centos-sclo-rh
devtoolset-7-gcc.x86_64                         7.3.1-5.16.el7    centos-sclo-rh
devtoolset-8-gcc.x86_64                         8.3.1-3.2.el7     centos-sclo-rh
devtoolset-9-gcc.x86_64                         9.3.1-2.2.el7     centos-sclo-rh


这些软件包可以同时安装,不会相互覆盖和冲突,也不会覆盖系统的版本。即可以在系统中可同时存在gcc 7, gcc 8, gcc 10等多个版本。结果如下

[root@Centos7 ~]# yum  -y install  devtoolset-10-gcc devtoolset-10-gcc-c++ 
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * centos-sclo-rh: mirrors.aliyun.com
 * centos-sclo-sclo: mirrors.ustc.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.aliyun.com
软件包 devtoolset-10-gcc-10.2.1-11.2.el7.x86_64 已安装并且是最新版本
软件包 devtoolset-10-gcc-c++-10.2.1-11.2.el7.x86_64 已安装并且是最新版本
无须任何处理

[root@Centos7 ~]# yum install  devtoolset-9-gcc devtoolset-9-gcc-c++
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * centos-sclo-rh: mirrors.aliyun.com
 * centos-sclo-sclo: mirrors.ustc.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.aliyun.com
软件包 devtoolset-9-gcc-9.3.1-2.2.el7.x86_64 已安装并且是最新版本
软件包 devtoolset-9-gcc-c++-9.3.1-2.2.el7.x86_64 已安装并且是最新版本
无须任何处理


因为不会覆盖系统默认的gcc,使用这些软件的方法有四种:

1.使用绝对路径;

[root@Centos7 ~]# rpm -qa |grep devtoolset
devtoolset-9-gcc-c++-9.3.1-2.2.el7.x86_64
devtoolset-10-gcc-10.2.1-11.2.el7.x86_64
devtoolset-9-libstdc++-devel-9.3.1-2.2.el7.x86_64
devtoolset-10-runtime-10.1-0.el7.x86_64
devtoolset-9-gcc-9.3.1-2.2.el7.x86_64
devtoolset-9-binutils-2.32-16.el7.x86_64
devtoolset-10-libstdc++-devel-10.2.1-11.2.el7.x86_64
devtoolset-10-binutils-2.35-5.el7.4.x86_64
devtoolset-10-gcc-c++-10.2.1-11.2.el7.x86_64
devtoolset-9-runtime-9.1-0.el7.x86_64
[root@Centos7 ~]# #查看安装路径
[root@Centos7 ~]# rpm -ql devtoolset-10-gcc-10.2.1-11.2.el7.x86_64
/opt/rh/devtoolset-10/root/usr/bin/cc
/opt/rh/devtoolset-10/root/usr/bin/cpp
/opt/rh/devtoolset-10/root/usr/bin/gcc
/opt/rh/devtoolset-10/root/usr/bin/gcc-ar
/opt/rh/devtoolset-10/root/usr/bin/gcc-nm
/opt/rh/devtoolset-10/root/usr/bin/gcc-ranlib
以下输出忽略

[root@Centos7 ~]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 



[root@Centos7 ~]# 
[root@Centos7 ~]# /opt/rh/devtoolset-10/root/usr/bin/gcc -v
Using built-in specs.
COLLECT_GCC=/opt/rh/devtoolset-10/root/usr/bin/gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-10/root/usr/libexec/gcc/x86_64-redhat-linux/10/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-10/root/usr --mandir=/opt/rh/devtoolset-10/root/usr/share/man --infodir=/opt/rh/devtoolset-10/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-10.2.1-20210130/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.1 20210130 (Red Hat 10.2.1-11) (GCC) 


[root@Centos7 ~]# /opt/rh/devtoolset-9/root/usr/bin/gcc -v
Using built-in specs.
COLLECT_GCC=/opt/rh/devtoolset-9/root/usr/bin/gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC) 


2.添加可执行文件路径到PATH环境变量;

3.使用官方推荐的加载命令:scl enable devtoolset-x bash, x为要启用的版本;

只对当前终端生效

[root@Centos7 ~]# scl enable devtoolset-10 bash 
[root@Centos7 ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-10/root/usr/libexec/gcc/x86_64-redhat-linux/10/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-10/root/usr --mandir=/opt/rh/devtoolset-10/root/usr/share/man --infodir=/opt/rh/devtoolset-10/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-10.2.1-20210130/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.1 20210130 (Red Hat 10.2.1-11) (GCC) 

4.执行安装软件自带的脚本: source /opt/rh/devtoolset-x/enable,x为要启用的版本。

[root@Centos7 ~]# 
[root@Centos7 ~]# source /opt/rh/devtoolset-9/enable 
[root@Centos7 ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC) 

实践推荐使用最后两种方式。

如果希望长期使用某个高版本,可将此命令写入.bashrc等配置文件。

其它

scl以及scl-rh源中的软件包都安装在/opt/rh/目录下,包含可执行文件、配置等。启用命令的路径是/opt/rh/xxx/enable,安装的服务重启命令则是systemctl restart rh-xxx,需要加rh或scl前缀以区别其他源的包。如果你用过remi/gitlab等源,其行为方式也是类似的。

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊酒弟弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值