CentOS7升级gcc/gcc-c++

  升级前需要知道自己需要的gcc版本,根据自己的需要进行升级。

  gcc版本和C++的对应关系可以参看gcc官网提供的对照表:https://gcc.gnu.org/projects/cxx-status.html

  gcc官网地址:GCC, the GNU Compiler Collection - GNU Project

  gcc源码下载地址:Index of /gnu/gcc

(1).实验环境

  4核8G  CentOS7.9.2009

  配置了阿里云yum源和yum -y install epel-release。

(2).yum升级(推荐)

  CentOS7已于2024年6月30日停止维护,导致官方的软件源(mirrorlist.centos.org)无法访问,所以我们要更换国内的镜像源(例如阿里云或清华源)。

  我这里使用阿里云的SCL仓库。

[root@localhost ~]# cd /etc/yum.repos.d/
# 如果有SCL的yum源,则备份原文件(以防万一)
[root@localhost yum.repos.d]# mv CentOS-SCLo-scl.repo CentOS-SCLo-scl.repo.bak 2>/dev/null
[root@localhost yum.repos.d]# mv CentOS-SCLo-scl-rh.repo CentOS-SCLo-scl-rh.repo.bak 2>/dev/null
#创建指向阿里云的SCL的yum源文件
[root@localhost yum.repos.d]# vim CentOS-SCLo-scl.repo
[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/sclo/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[root@localhost yum.repos.d]# vim CentOS-SCLo-scl-rh.repo
[centos-sclo-rh]
name=CentOS-7 - SCLo rh
baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/rh/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
#下载GPG密钥,这个是官网地址
[root@localhost yum.repos.d]# curl -o /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo https://www.centos.org/keys/RPM-GPG-KEY-CentOS-SIG-SCLo
#导入密钥到RPM库
[root@localhost yum.repos.d]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
#清除缓存并重新加载
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache

  yum安装前可以看下阿里云的SCL仓库支持版本:https://mirrors.aliyun.com/centos/7/sclo/x86_64/sclo/,gcc就支持7~11版本。

#安装11版的gcc和gcc-c++
[root@localhost yum.repos.d]# yum -y install devtoolset-11-gcc devtoolset-11-gcc-c++

  此时还需要激活,否则gcc和gcc-c++还是使用的老版本

[root@localhost yum.repos.d]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[root@localhost yum.repos.d]# g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

#临时激活devtoolset-11软件集合,一旦重启就会失效
#也可以用source /opt/rh/devtoolset-11/enable来进行激活,这个命令写入环境变量就可以持久化
[root@localhost yum.repos.d]# scl enable devtoolset-11 bash
[root@localhost yum.repos.d]# gcc --version
gcc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[root@localhost yum.repos.d]# g++ --version
g++ (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  持久化激活

[root@localhost yum.repos.d]# echo "source /opt/rh/devtoolset-11/enable" >> /etc/profile

(3).源码编译升级

  源码编译安装时,gcc的依赖可以在源码目录下的contrib/download_prerequisites查看,并且可以统一在Index of /pub/gcc/infrastructure下载。其中gmp、mpfr、mpc也可以在Index of /gnu进行下载。不要低于要求的版本。

  我这里展示的是gcc-15.2.0版本的依赖要求。

image

 

  如果低于gcc11,还是建议用yum安装,如果要安装gcc12及以上版本,再使用源码编译安装。

  因为gcc高版本编译时需要的C++版本会比较高,而CentOS7在不使用SCL仓库的情况下,gcc的版本最多只能到4.8.5,对应到C++版本就是C++11。所以要先yum安装gcc11,否则连编译都无法完成。这个直接看上面,但不要永久激活,只临时激活使用一下。

  编译安装gcc

[root@localhost ~]# tar xvf gcc-15.2.0.tar.xz
[root@localhost ~]# cd gcc-15.2.0
#你可以将下载好的依赖包放入gcc-15.2.0目录,并逐个解压
#也可以直接用./contrib/download_prerequisites,让gcc脚本自动下载
#下载时间稍微有点长,我这里将下载好的安装包放入并解压,然后重命名
[root@localhost gcc-15.2.0]# tar xvf gmp-6.3.0.tar.xz
[root@localhost gcc-15.2.0]# mv gmp-6.3.0 gmp
[root@localhost gcc-15.2.0]# tar xvf mpfr-4.2.2.tar.xz
[root@localhost gcc-15.2.0]# mv mpfr-4.2.2 mpfr
[root@localhost gcc-15.2.0]# tar zxvf mpc-1.3.1.tar.gz
[root@localhost gcc-15.2.0]# mv mpc-1.3.1 mpc
[root@localhost gcc-15.2.0]# tar jxvf isl-0.24.tar.bz2
[root@localhost gcc-15.2.0]# mv isl-0.24 isl
[root@localhost gcc-15.2.0]# tar xvf gettext-0.22.tar.gz
[root@localhost gcc-15.2.0]# mv gettext-0.22 gettext
[root@localhost gcc-15.2.0]# mkdir tmp && cd tmp
#理论上来说这一步不写,或写为/usr会直接覆盖原本的gcc [root@localhost tmp]# ../configure --prefix=/usr/local/gcc-15.2.0 --enable-language=c,c++,java --disable-multilib [root@localhost tmp]# echo $? 0 #编译时间会相当长,耐心等待 [root@localhost tmp]# make -j$(nproc) [root@localhost tmp]# echo $? 0 [root@localhost tmp]# make install [root@localhost tmp]# echo $? 0

  重新开一个窗口,或者重启服务器,让devtoolset-11失效。如果在使用时能指定gcc的目录,直接指定最好。实在无法指定再使用一下方法,因为软链接这部分能不能生效我无法验证。有大佬能验证的踢我一下,为我解惑。

[root@localhost ~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[root@localhost ~]# mv /usr/lib64/libstdc++.so.6{,.bak}
[root@localhost ~]# ln -s /usr/local/gcc-15.2.0/lib64/libstdc++.so.6.0.34 /usr/lib64/libstdc++.so.6
[root@localhost ~]# mv /usr/bin/gcc{,.bak}
[root@localhost ~]# ln -s /usr/local/gcc-15.2.0/bin/gcc /usr/bin/gcc
[root@localhost ~]# mv /usr/bin/g++{,.bak}
[root@localhost ~]# ln -s /usr/local/gcc-15.2.0/bin/g++ /usr/bin/g++
[root@localhost ~]# mv /usr/bin/c++{,.bak}
[root@localhost ~]# ln -s /usr/local/gcc-15.2.0/bin/c++ /usr/bin/c++
[root@localhost ~]# mv /usr/bin/cc{,.bak}
[root@localhost ~]# ln -s /usr/local/gcc-15.2.0/bin/cc /usr/bin/cc
[root@localhost ~]# gcc --version
gcc (GCC) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[root@localhost ~]# g++ --version
g++ (GCC) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  

  

 

参考:https://blog.csdn.net/whc18858/article/details/135484071

   https://blog.csdn.net/xuwei1210/article/details/145700186

posted @ 2026-01-20 17:34  苦逼运维  阅读(0)  评论(0)    收藏  举报