gcc升级(含命令行升级、手动升级两种方式)

2025-10-16 06:04:22 策略智库

gcc升级

1.yum源替换1.1 备份原始repo配置文件1.2 重新配置CentOS-Base.reporepo文件1.3 清除缓存并重新创建

2. gcc安装3.命令行升级gcc4.手动升级4.1 安装包下载4.2 解压4.3 gcc升级4.3.1 依赖拉取4.3.2 gmp安装4.3.3 mpfr安装4.3.4 mpc安装4.3.5 gcc编译、安装

4.4 gcc命令配置

1.yum源替换

默认yum源不可用,替换为国内源 替换前:

1.1 备份原始repo配置文件

cd /etc/yum.repos.d/

mkdir bak

mv CentOS-* bak/

1.2 重新配置CentOS-Base.reporepo文件

若服务器有wget命令,可复制下述命令直接从互联网拉取阿里配置源到服务器

wget https://mirrors.aliyun.com/repo/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo

若服务器为最小化安装,没有wget命令,可以直接在/etc/yum.repos.d/目录下,创建名称为CentOS-Base.repo的文件,把下述内容复制到配置文件中

CentOS-Base.reporepo全内容

[base]

name=CentOS-$releasever - Base - mirrors.aliyun.com

failovermethod=priority

baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/

http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/

http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/

gpgcheck=1

gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#released updates

[updates]

name=CentOS-$releasever - Updates - mirrors.aliyun.com

failovermethod=priority

baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/

http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/

http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/

gpgcheck=1

gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras - mirrors.aliyun.com

failovermethod=priority

baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/

http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/

http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/

gpgcheck=1

gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus - mirrors.aliyun.com

failovermethod=priority

baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/

http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/

http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#contrib - packages by Centos Users

[contrib]

name=CentOS-$releasever - Contrib - mirrors.aliyun.com

failovermethod=priority

baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/

http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/

http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

修改后:

1.3 清除缓存并重新创建

yum clean all

yum makecache

2. gcc安装

yum -y install gcc gcc-c++

安装后,默认版本为:4.8.5

3.命令行升级gcc

yum -y install centos-release-scl

yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutilse

scl enable devtoolset-9 bash

echo "source /opt/rh/devtoolset-9/enable">> /etc/profile

执行过程中出现异常信息: 1.安装:centos-release-scl时正常,安装:devtoolset-9-gcc、devtoolset-9-gcc-c++、devtoolset-9-binutilse时出现报错 解决方法: 查看报错信息,报错为:CentOS-SCLo-scl-rh.repo文件地址无法访问 使用阿里对应的配置文件,修改改文件内容 CentOS-SCLo-scl-rh.repo文件全内容

[centos-sclo-rh]

name=CentOS-$releasever - SCLo rh

baseurl=https://mirrors.aliyun.com/centos/$releasever/sclo/$basearch/rh/

gpgcheck=1

enabled=1

gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

CentOS-SCLo-scl.repo文件内容

[centos-sclo-sclo]

name=CentOS-$releasever - SCLo sclo

baseurl=https://mirrors.aliyun.com/centos/$releasever/sclo/$basearch/sclo/

gpgcheck=1

enabled=1

gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

2.清除缓存,重新创建缓存后,再次安装devtoolset-9-gcc、devtoolset-9-gcc-c++、devtoolset-9-binutilse出现异常信息 解决方法: 查看报错信息为密钥问题,尝试在安装时候,直接安装,不校验密钥

yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutilse --nogpgcheck

成功安装 在当前shell终端中使devtoolset-9生效,配置永久使用devtoolset-9,查看gcc版本,已经成功升级到9.3.1

4.手动升级

以9.3.0为例

4.1 安装包下载

# 创建存放目录

mkdir -p /apps/scripts

cd /apps/scripts/

# gcc安装包下载

下载地址:https://mirrors.aliyun.com/gnu/gcc/gcc-9.3.0/gcc-9.3.0.tar.gz

可下载后上传,或直接在服务器上从互联网拉取

wget https://mirrors.aliyun.com/gnu/gcc/gcc-9.3.0/gcc-9.3.0.tar.gz

4.2 解压

tar -xzf gcc-9.3.0.tar.gz

cd gcc-9.3.0

[root@rhel76 gcc-10.1.0]# mkdir build

[root@rhel76 gcc-10.1.0]# cd build/

[root@rhel76 build]# …/configure --prefix=/usr/local/gcc-10.1.0/ --enable-checking=release --enable-languages=c,c++ --disable-multilib

4.3 gcc升级

4.3.1 依赖拉取

cd /apps/scripts/gcc-9.3.0/

./contrib/download_prerequisites

// 在gcc-9.3.0目录下,多了gmp-6.1.0.tar.bz2、isl-0.18.tar.bz2、mpc-1.0.3.tar.gz、mpfr-3.1.4.tar.bz2四个压缩文件

// 若已经安装了bzip2命令,则会自动解压上述压缩包

// 演示环境为最小化安装,目前未安装bzip2命令

4.3.2 gmp安装

解压gmp

cd /apps/scripts/gcc-9.3.0/

tar -xvjf gmp-6.1.0.tar.bz2

解压报错:

tar (child): bzip2: Cannot exec: No such file or directory

tar (child): Error is not recoverable: exiting now

tar: Child returned status 2

tar: Error is not recoverable: exiting now

安装bzip2后,重新解压

安装bzip2

yum -y install bzip2 m4

// bzip2:解决上述报错 m4:解决configure过程中报错

继续安装gmp

tar -xvjf gmp-6.1.0.tar.bz2

cd /apps/scripts/gcc-9.3.0/gmp-6.1.0

./configure --prefix=/usr/local/gmp-6.1.0

make

make install

解压完成 configure 完成 make完成 make install完成

4.3.3 mpfr安装

cd /apps/scripts/gcc-9.3.0/

tar -xvjf mpfr-3.1.4.tar.bz2

cd mpfr-3.1.4

./configure --prefix=/usr/local/mpfr-3.1.4 --with-gmp=/usr/local/gmp-6.1.0

make

make install

解压完成 configure 完成 make完成 make install 完成

4.3.4 mpc安装

cd /apps/scripts/gcc-9.3.0/

tar -xvf mpc-1.0.3.tar.gz

cd mpc-1.0.3

./configure --prefix=/usr/local/mpc-1.0.3 --with-gmp=/usr/local/gmp-6.1.0 --with-mpfr=/usr/local/mpfr-3.1.4

make

make install

解压完成 configure 完成 make完成 make install 完成

4.3.5 gcc编译、安装

cd /apps/scripts/gcc-9.3.0

mkdir build

cd build/

../configure --prefix=/usr/local/gcc-9.3.0/ --enable-checking=release --enable-languages=c,c++ --disable-multilib --with-gmp=/usr/local/gmp-6.1.0 --with-mpfr=/usr/local/mpfr-3.1.4 --with-mpc=/usr/local/mpc-1.0.3

make

yum -y remove gcc gcc-c++

make install

configure 完成 make完成–很久很久 build目录下对应文件大小 旧版本删除完成 GCC安装完成

4.4 gcc命令配置

ln -s /usr/local/gcc-9.3.0/bin/gcc /usr/bin/gcc

ln -s /usr/local/gcc-9.3.0/bin/g++ /usr/bin/g++

gcc -v