CentOS 更新gcc+glibc环境

由于centos环境较老,基础环境已经难以跟上主流的第三方库,在此对系统基础环境进行升级并主要记录通过centos源码升级gcc、glibc的过程和踩坑。

环境准备

· 系统: CentOS7

· gcc: 12.4

· glibc: 2.34

升级GCC

源码下载

gcc资源合集:https://ftp.gnu.org/gnu/gcc/

找到需要的版本,下载tar.gz文件,此处以gcc-12.4.0为例

1
2
wget http://ftp.gnu.org/gnu/gcc/gcc-12.4.0/gcc-12.4.0.tar.gz
tar -zxvf gcc-12.4.0.tar.gz

源码编译

1
2
3
4
5
cd gcc-12.4.0
mkdir build
cd build
../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib
make -j 8

make过程时间较长,可能出现报错,例如:

  • cc1:all warnings being treated as errors:由于系统内核版本原因造成的错误,未使用的变量警告被视为错误,导致编译失败。

    解决方案:删除build下文件夹,重新执行../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib获得初始文件,全局搜索-Werror并移除、将相关参数设置为no,并打开MakeFile文件写入:

    1
    2
    CFLAGS = -Wall -Wpointer-arith -Wno-unused
    KBUILD_CFLAGS += -w

    重新执行make操作

  • configure: error: C compiler cannot create executables:可能是系统丢失了文件。

    解决方案:到相同服务器拷贝/usr/lib64/libc_nonshared.a文件至/usr/lib64下确实解决了

  • 找不到ld文件:可能源自先前通过devtoolset安装的gcc环境没有删干净,例如在/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9下缺失ld文件,可将/usr/bin下ld文件复制到/opt/rh/devtoolset-9/root/usr/bin

    如果devtoolset-9已经卸载,需要重新安装:

    yum install devtoolset-9-gcc*

    成功后再卸载:

    yum remove devtoolset-9-gcc*

源码安装

先卸载先前系统gcc环境再安装

1
2
yum -y remove gcc g++
make install

验证版本

1
2
gcc -v
/usr/local/bin/gcc -v
1
2
3
4
5
6
7
8
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/12.4.0/lto-wrapper
目标:x86_64-pc-linux-gnu
配置为:../configure --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++,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --enable-gnu-indirect-function --with-tune=generic --disable-multilib
线程模型:posix
Supported LTO compression algorithms: zlib
gcc 版本 12.4.0 (GCC)

配置全局环境

1
2
3
4
5
6
ln -s /usr/local/bin/gcc /usr/bin/gcc
ln -s /usr/local/bin/g++ /usr/bin/g++

# 更新动态库
rm -f /usr/lib64/libstdc++.so.6
ln -s /usr/local/lib64/libstdc++.so.6.0.28 /usr/lib64/libstdc++.so.6

升级Glibc

源码下载

gcc资源合集:https://ftp.gnu.org/gnu/glibc/

找到需要的版本,下载tar.gz文件,此处以glibc-2.34为例

1
2
wget https://ftp.gnu.org/gnu/glibc/glibc-2.34.tar.gz
tar -zxvf glibc-2.34.tar.gz

查看glibc安装的具体要求,可查看服务器是否满足,主要关注gcc、make和python是否满足:

1
2
cd glibc-2.34
cat INSTALL | grep -E "later|newer"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     this option if you want to compile the GNU C Library with a newer
later. Note that when CET is enabled, the GNU C Library requires
Intel Pentium Pro or newer. With '--enable-cet', it is an error to
to build without this option (for example, if building with a newer
The tests (and later installation) use some pre-existing files of the
* GNU 'make' 4.0 or newer
* GCC 6.2 or newer
building the GNU C Library, as newer compilers usually produce
of release, this implies GCC 7.4 and newer (excepting GCC 7.5.0,
* GNU 'binutils' 2.25 or later
binutils 2.26 or newer.
* 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
to have the header files from a 3.2 or newer kernel around for
reference. (For the ia64 architecture, you need version 3.2.18 or newer

源码编译

1
2
3
4
5
cd glibc-2.34
mkdir bulid
cd bulid
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
make -j 8

make过程中可能会遇到gcc make时相似的问题,相同方式解决

源码安装

1
make install

此时会出现报错:

gcc: relocation error: /lib64/libc.so.6: symbol __tunable_get_val, version GLIBC_PRIVATE not defined in file ld-linux-x86-64.so.2 with link time reference,且系统中的基础命令不可用。需要执行:

1
2
3
4
LD_PRELOAD=/lib64/libc-2.34.so sln /usr/local/glibc-2.34/build/libc.so.6                 /lib64/libc.so.6
LD_PRELOAD=/lib64/libc-2.34.so sln /usr/local/glibc-2.34/build/dlfcn/libdl.so.2 /lib64/libdl.so.2
LD_PRELOAD=/lib64/libc-2.34.so sln /usr/local/glibc-2.34/build/nptl/libpthread.so.0 /lib64/libpthread.so.0
LD_PRELOAD=/lib64/libc-2.34.so sln /usr/local/glibc-2.34/build/elf/ld-linux-x86-64.so.2 /usr/lib64/ld-linux-x86-64.so.2


再次make install,完成安装

### 安装验证

1
strings /lib64/libc.so.6 | grep GLIBC


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
32
33
34
35
36
GLIBC_2.2.5
GLIBC_2.2.6
GLIBC_2.3
GLIBC_2.3.2
GLIBC_2.3.3
GLIBC_2.3.4
GLIBC_2.4
GLIBC_2.5
GLIBC_2.6
GLIBC_2.7
GLIBC_2.8
GLIBC_2.9
GLIBC_2.10
GLIBC_2.11
GLIBC_2.12
GLIBC_2.13
GLIBC_2.14
GLIBC_2.15
GLIBC_2.16
GLIBC_2.17
GLIBC_2.18
GLIBC_2.22
GLIBC_2.23
GLIBC_2.24
GLIBC_2.25
GLIBC_2.26
GLIBC_2.27
GLIBC_2.28
GLIBC_2.29
GLIBC_2.30
GLIBC_2.31
GLIBC_2.32
GLIBC_2.33
GLIBC_2.34
GLIBC_PRIVATE
...


### 问题排查

升级glibc时,如果发生错误:

- error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

且基础命令失效,可执行:

LD_PRELOAD=/lib64/libc-2.28.so ln -sf /lib64/libc-2.28.so /lib64/libc.so.6

注:libc-2.28为系统已有的,此状态不可以退出shell或重启

- Inconsistency detected by ld.so: dl-call-libc-early-init.c: 37: _dl_call_libc_early_init: Assertionsym != NULL’ failed!`

且基础命令失效,可执行:

1
2
3
export LD_LIBRARY_PATH=""
echo "" > /etc/ld.so.conf.d/glibc-2.28.conf
ldconfig

进行恢复

注:libc-2.28为系统已有的,此状态不可以退出shell或重启

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2025 青域 All Rights Reserved.

UV : | PV :