centos 7 升级gcc、glibc

发布于 2024-04-07  65 次阅读


centos 7 升级gcc、glibc

1、yum 方式升级

# 1.查看版本号
[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.
# 2.安装centos高级库
[root@localhost ~]# yum install centos-release-scl
# 3.安装高版本gcc
[root@localhost ~]# yum install -y devtoolset-8-gcc devtoolset-8-gcc-c++ devtoolset-8-binutils
# 4.加载环境变量
[root@localhost ~]# echo "source /opt/rh/devtoolset-8/enable" >> /etc/profile && source /opt/rh/devtoolset-8/enable
[root@localhost ~]# gcc --version
gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
Copyright (C) 2018 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.

2、编译方式升级

2.1 升级make

# 1.安装依赖
[root@localhost ~]# yum -y install gcc gcc-c++
# 2.建立安装包存放目录
[root@localhost ~]# mkdir /backup
# 3.下载make安装包
[root@localhost ~]# cd /backup
[root@localhost ~]# wget https://mirrors.aliyun.com/gnu/make/make-4.3.tar.gz
# 4.解压压缩包并建立构建目录
[root@localhost ~]# tar -xf make-4.3.tar.gz
[root@localhost ~]# cd make-4.3
[root@localhost ~]# mkdir build
[root@localhost ~]# cd build
# 5.指定安装到具体的目录下,此示例表示将make安装到/opt下
[root@localhost ~]# ../configure --prefix=/opt/make
# 6.编译安装
[root@localhost ~]# make && make install
# 7.建立软连接
[root@localhost ~]# ln -sf /opt/make/bin/make /usr/bin/make
# 8.检查make版本
make --version
GNU Make 4.3

2.2 升级gcc

# 1.安装升级依赖
[root@localhost ~]# yum install -y gcc-c++ glibc-devel mpfr-devel libmpc-devel gmp-devel glibc-devel.i686 bzip2

# 2.下载gcc9.3.1安装包
[root@localhost ~]# cd /backup
[root@localhost ~]# wget https://ftp.gnu.org/gnu/gcc/gcc-9.3.0/gcc-9.3.0.tar.gz --no-check-certificate

# 3.解包并执行编译前的准备
[root@localhost ~]# tar -xf gcc-9.3.0.tar.gz
[root@localhost ~]# cd gcc-9.3.0
# 4.下载依赖包
[root@localhost gcc-9.3.0]# ./contrib/download_prerequisites
gmp-6.1.0.tar.bz2: OK
mpfr-3.1.4.tar.bz2: OK
mpc-1.0.3.tar.gz: OK
isl-0.18.tar.bz2: OK
All prerequisites downloaded successfully.
# 5.建立构建目录
[root@localhost gcc-9.3.0]# mkdir build
# 6.进入构建目录
[root@localhost gcc-9.3.0]# cd build
​
# 7.指定安装到具体的目录下,此示例表示将make安装到/usr下(说明:若安装到非/usr目录,如安装到/opt/gcc,则在编译完成后需要配置环境变量、建立软连接。)
[root@localhost build]# ../configure --enable-checking=release --enable-language=c,c++ --disable-multilib --prefix=/usr
​
# 8.编译安装
[root@localhost build]# make
[root@localhost build]# make install

# 9.配置环境变量
[root@localhost build]# vi /etc/profile.d/gcc.sh
export PATH=/opt/gcc/bin:$PATH
export LD_LIBRARY_PATH=/opt/gcc/lib
# 编辑完成后:wq保存并退出
# 10.重载环境变量
[root@localhost build]# source /etc/profile
# 11.重新生成新的链接
# 12.取消原始链接
[root@localhost build]# unlink /usr/bin/cc
# 13.建立新链接
[root@localhost build]# ln -sf /opt/gcc/bin/gcc /usr/bin/cc
[root@localhost build]# ln -sf /opt/gcc/lib/gcc/x86_64-pc-linux-gnu/9.3.0/include /usr/include/gcc
# 14.设置库文件
[root@localhost build]# echo "/opt/gcc/lib64" >> /etc/ld.so.conf.d/gcc.conf
# 15.加载动态连接库
[root@localhost build]# ldconfig -v
# 16.查看加载结果
[root@localhost build]# ldconfig -p | grep gcc
​
# 17.安装完成后检查gcc版本,若gcc升级失败则需查找失败原因并重新进行升级操作
[root@localhost build]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/9.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --enable-checking=release --enable-language=c,c++ --disable-multilib --prefix=/usr
Thread model: posix
gcc version 9.3.0 (GCC)

3、glibc升级

# 1.查看当前版本
[root@localhost ~]# yum install -y python3 python3-devel bison
[root@localhost ~]# strings /lib64/libc.so.6 | grep -E "^GLIBC" | sort -V -r | uniq
GLIBC_PRIVATE
GLIBC_2.17
GLIBC_2.16
GLIBC_2.15
GLIBC_2.14
GLIBC_2.13
GLIBC_2.12
GLIBC_2.11
GLIBC_2.10
GLIBC_2.9
GLIBC_2.8
GLIBC_2.7
GLIBC_2.6
GLIBC_2.5
GLIBC_2.4
GLIBC_2.3.4
GLIBC_2.3.3
GLIBC_2.3.2
GLIBC_2.3
GLIBC_2.2.6
GLIBC_2.2.5

# 2.
# 2.下载glibc-2.31安装包
[root@localhost ~]# mkdir /backup && cd /backup
[root@localhost ~]# wget https://mirrors.aliyun.com/gnu/glibc/glibc-2.31.tar.gz

# 3.进入到解压目录
[root@localhost ~]# tar -xf glibc-2.31.tar.gz
[root@localhost ~]# cd glibc-2.31

# 4.查看安装glibc的前提依赖,对于不满足的依赖需要进行升级,使用yum -y install xxx 升级或安装即可
[root@localhost ~]# cat INSTALL | grep -E "newer|later" | grep "*"
# 输出
* GNU 'make' 4.0 or newer
* GCC 6.2 or newer
* GNU 'binutils' 2.25 or later
* GNU 'texinfo' 4.7 or later
* GNU 'bison' 2.7 or later
* GNU 'sed' 3.02 or newer
* Python 3.4 or later
* GDB 7.8 or later with support for Python 2.7/3.4 or later
* GNU 'gettext' 0.10.36 or later
# 假设上述依赖条件已全部满足

# 5.建立构建目录,执行编译安装
[root@localhost ~]# mkdir build

# 6.指定安装到具体的目录下,此示例表示将make安装到/opt下
[root@localhost ~]# cd build/
[root@localhost ~]# ../configure  --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin --disable-sanity-checks --disable-werror

# 7.编译安装
[root@localhost ~]# make
[root@localhost ~]# make install
# 解决新启动远程终端时报一个WARNING
[root@localhost ~]# make localedata/install-locales

# install结束会出现一个错误,此错误可忽略
# 错误输出
Execution of gcc -B/usr/bin/ failed!
The script has found some problems with your installation!
Please read the FAQ and the README file and check the following:
- Did you change the gcc specs file (necessary after upgrading from
  Linux libc5)?
- Are there any symbolic links of the form libXXX.so to old libraries?
  Links like libm.so -> libm.so.5 (where libm.so.5 is an old library) are wrong,
  libm.so should point to the newly installed glibc file - and there should be
  only one such link (check e.g. /lib and /usr/lib)
You should restart this script from your build directory after you've
fixed all problems!
Btw. the script doesn't work if you're installing GNU libc not as your
primary library!
make[1]: *** [Makefile:120: install] Error 1
make[1]: Leaving directory '/backup/glibc-2.31'
make: *** [Makefile:12: install] Error 2'

# 8.安装完成后检查glibc版本
[root@localhost ~]# strings /lib64/libc.so.6 | grep -E "^GLIBC" | sort -V -r | uniq
# 输出
GLIBC_PRIVATE
GLIBC_2.30
GLIBC_2.29
GLIBC_2.28
GLIBC_2.27
GLIBC_2.26
GLIBC_2.25
GLIBC_2.24
GLIBC_2.23
GLIBC_2.22
GLIBC_2.18
GLIBC_2.17
GLIBC_2.16
GLIBC_2.15
GLIBC_2.14
GLIBC_2.13
GLIBC_2.12
GLIBC_2.11
GLIBC_2.10
GLIBC_2.9
GLIBC_2.8
GLIBC_2.7
GLIBC_2.6
GLIBC_2.5
GLIBC_2.4
GLIBC_2.3.4
GLIBC_2.3.3
GLIBC_2.3.2
GLIBC_2.3
GLIBC_2.2.6
GLIBC_2.2.5