Skip to content

现象

在Centos上启动某程序的时候报GLIBCXX_3.4.** not found的错误,记录解决错误的过程

1.查看当前版本

bash
[root@localhost [10.10.60.21] /]# strings /usr/lib64/libstdc++.so.6 |grep GLIBC
GLIBCXX_3.4
GLIBCXX_3.4.1
   ......
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
[root@localhost [10.10.60.21] /]#
[root@localhost [10.10.60.21] /]# strings /usr/lib64/libstdc++.so.6 |grep GLIBC
GLIBCXX_3.4
GLIBCXX_3.4.1
   ......
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
[root@localhost [10.10.60.21] /]#

存在相互依赖,所以安装的顺序依次为:gmp,mpfr,mpc

2.安装

2.1下载

bash
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz

2.2依赖安装

bash
## 非root用户,因此将安装目录配置到用户目录下
## 未指定将会安装到 /usr/local/lib 目录下

#### 安装GMP
##################################
tar -jxvf gmp-6.1.0.tar.bz2
cd gmp-6.1.0
./configure  --prefix=/data/apps/local/gmp-6.1.0
make -j4
sudo make install

#### 安装MPFR
##################################
tar -jxvf mpfr-3.1.4.tar.bz2
cd mpfr-3.1.4
./configure  --prefix=/data/apps/local/mpfr-3.1.4 \
             --with-gmp=/data/apps/local/gmp-6.1.0
make 
sudo make install

#### 安装MPC
##################################
tar -zxvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure  --prefix=/data/apps/local/mpc-1.0.3 \
             --with-gmp=/data/apps/local/gmp-6.1.0 \
             --with-mpfr=/data/apps/local/mpfr-3.1.4
make 
sudo make install
## 非root用户,因此将安装目录配置到用户目录下
## 未指定将会安装到 /usr/local/lib 目录下

#### 安装GMP
##################################
tar -jxvf gmp-6.1.0.tar.bz2
cd gmp-6.1.0
./configure  --prefix=/data/apps/local/gmp-6.1.0
make -j4
sudo make install

#### 安装MPFR
##################################
tar -jxvf mpfr-3.1.4.tar.bz2
cd mpfr-3.1.4
./configure  --prefix=/data/apps/local/mpfr-3.1.4 \
             --with-gmp=/data/apps/local/gmp-6.1.0
make 
sudo make install

#### 安装MPC
##################################
tar -zxvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure  --prefix=/data/apps/local/mpc-1.0.3 \
             --with-gmp=/data/apps/local/gmp-6.1.0 \
             --with-mpfr=/data/apps/local/mpfr-3.1.4
make 
sudo make install
  • 安装完以上,添加环境变量

vim /etc/profile

bash
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/data/apps/local/mpc-1.0.3/mpc_install/lib:/data/apps/local/gmp-6.1.0/gmp_install/lib:/data/apps/local/mpfr-3.1.4/mpfr_install/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/data/apps/local/gcc5.5.0/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/data/apps/local/mpc-1.0.3/mpc_install/lib:/data/apps/local/gmp-6.1.0/gmp_install/lib:/data/apps/local/mpfr-3.1.4/mpfr_install/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/data/apps/local/gcc5.5.0/lib64:$LD_LIBRARY_PATH

source /etc/profile

2.3gcc安装

bash
wget ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-5.5.0/gcc-5.5.0.tar.gz
tar -zxvf gcc-5.5.0.tar.gz
cd gcc-5.5.0
./configure  --prefix=/data/apps/local/gcc5.5.0  --enable-checking=release --enable-languages=c,c++,java --enable-threads=posix --disable-multilib
make -j4
make install


----注释
./configure \
    --prefix=/home/xxx/local/gcc5.5.0 \   # 指定安装路径
    --enable-checking=release \         # 生成的编译器在编译过程中不做额外检查
    --enable-languages=c,c++,java \     # 支持的语言
    --enable-threads=posix \            # 使用POSIX/Unix98作为线程支持库
    --disable-multilib                  # 不生成编译为其他平台可执行代码的交叉编译器
wget ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-5.5.0/gcc-5.5.0.tar.gz
tar -zxvf gcc-5.5.0.tar.gz
cd gcc-5.5.0
./configure  --prefix=/data/apps/local/gcc5.5.0  --enable-checking=release --enable-languages=c,c++,java --enable-threads=posix --disable-multilib
make -j4
make install


----注释
./configure \
    --prefix=/home/xxx/local/gcc5.5.0 \   # 指定安装路径
    --enable-checking=release \         # 生成的编译器在编译过程中不做额外检查
    --enable-languages=c,c++,java \     # 支持的语言
    --enable-threads=posix \            # 使用POSIX/Unix98作为线程支持库
    --disable-multilib                  # 不生成编译为其他平台可执行代码的交叉编译器

2.3替换原来lib

  • 查找所有lib
[root@jump lib64]# find / -name "libstdc++.so*"
/opt/rh/devtoolset-9/root/usr/lib/gcc/x86_64-redhat-linux/9/32/libstdc++.so
/opt/rh/devtoolset-9/root/usr/lib/gcc/x86_64-redhat-linux/9/libstdc++.so
/data/apps/local/gcc5.5.0/lib64/libstdc++.so.6.0.21-gdb.py
/data/apps/local/gcc5.5.0/lib64/libstdc++.so.6.0.21

#新的
/data/apps/local/gcc5.5.0/lib64/libstdc++.so
/data/apps/local/gcc5.5.0/lib64/libstdc++.so.6
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.py
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.pyo
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.pyc

#旧的
/usr/lib64/libstdc++.so.6
/usr/lib64/libstdc++.so.6.0.19
[root@jump lib64]# find / -name "libstdc++.so*"
/opt/rh/devtoolset-9/root/usr/lib/gcc/x86_64-redhat-linux/9/32/libstdc++.so
/opt/rh/devtoolset-9/root/usr/lib/gcc/x86_64-redhat-linux/9/libstdc++.so
/data/apps/local/gcc5.5.0/lib64/libstdc++.so.6.0.21-gdb.py
/data/apps/local/gcc5.5.0/lib64/libstdc++.so.6.0.21

#新的
/data/apps/local/gcc5.5.0/lib64/libstdc++.so
/data/apps/local/gcc5.5.0/lib64/libstdc++.so.6
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.py
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.pyo
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.pyc

#旧的
/usr/lib64/libstdc++.so.6
/usr/lib64/libstdc++.so.6.0.19
  • 查看旧的版本
bash
strings /usr/lib64/libstdc++.so.6 | grep GLIBC
strings /usr/lib64/libstdc++.so.6 | grep GLIBC
  • 替换
bash
ll /usr/lib64/libstdc++.so.6
lrwxrwxrwx 1 root root 19 Jan  9 13:27 /usr/lib64/libstdc++.so.6 -> libstdc++.so.6.0.19

#删除软链
rm /usr/lib64/libstdc++.so.6

#生成新的
ln -sv /data/apps/local/gcc5.5.0/lib64/libstdc++.so.6.0.21 /usr/lib64/libstdc++.so.6
ll /usr/lib64/libstdc++.so.6
lrwxrwxrwx 1 root root 19 Jan  9 13:27 /usr/lib64/libstdc++.so.6 -> libstdc++.so.6.0.19

#删除软链
rm /usr/lib64/libstdc++.so.6

#生成新的
ln -sv /data/apps/local/gcc5.5.0/lib64/libstdc++.so.6.0.21 /usr/lib64/libstdc++.so.6

至此结束

3.gcc升级

3.1alibabacloud2

运行:yum install centos-release-scl

结果得到:No package centos-release-scl available.

大概是某些不可描述的原因。

bash
cat /etc/redhat-release
得到的版本是:
`Alibaba Cloud Linux (Aliyun Linux) release 2.1903 LTS (Hunting Beagle)`
cat /etc/redhat-release
得到的版本是:
`Alibaba Cloud Linux (Aliyun Linux) release 2.1903 LTS (Hunting Beagle)`

解决方式:

bash
rpm -ivh https://cbs.centos.org/kojifiles/packages/centos-release-scl-rh/2/3.el7.centos/noarch/centos-release-scl-rh-2-3.el7.centos.noarch.rpm

rpm -ivh https://cbs.centos.org/kojifiles/packages/centos-release-scl/2/3.el7.centos/noarch/centos-release-scl-2-3.el7.centos.noarch.rpm
rpm -ivh https://cbs.centos.org/kojifiles/packages/centos-release-scl-rh/2/3.el7.centos/noarch/centos-release-scl-rh-2-3.el7.centos.noarch.rpm

rpm -ivh https://cbs.centos.org/kojifiles/packages/centos-release-scl/2/3.el7.centos/noarch/centos-release-scl-2-3.el7.centos.noarch.rpm

3.2centos系列

1.安装源

bash
yum -y install centos-release-scl

yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
yum -y install centos-release-scl

yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash

需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。 如果要长期使用gcc 9.3的话:

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

2.查看版本

bash
[root@k8s-node01 tengine-3.0.0]# 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)
[root@k8s-node01 tengine-3.0.0]# 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)

4.make升级

bash
$ wget --no-check-certificate https://ftp.gnu.org/gnu/make/make-4.3.tar.gz
$ tar -xzvf make-4.3.tar.gz 
$ cd make-4.3/
 
# 安装到指定目录
$ ./configure  --prefix=/usr/local/make
$ make
$ make install
 
# 创建软链接
$ cd /usr/bin/
$ mv make make.bak # backup
$ ln -sv /usr/local/make/bin/make /usr/bin/make
$ wget --no-check-certificate https://ftp.gnu.org/gnu/make/make-4.3.tar.gz
$ tar -xzvf make-4.3.tar.gz 
$ cd make-4.3/
 
# 安装到指定目录
$ ./configure  --prefix=/usr/local/make
$ make
$ make install
 
# 创建软链接
$ cd /usr/bin/
$ mv make make.bak # backup
$ ln -sv /usr/local/make/bin/make /usr/bin/make

5.升级python

bash
[root@jump node20.10]# which python3
/usr/bin/python3
[root@jump node20.10]# rm /usr/bin/python
rm: remove symbolic link ‘/usr/bin/python’? y
[root@jump node20.10]# ln -sv /usr/bin/python3 /usr/bin/python
‘/usr/bin/python’ -> ‘/usr/bin/python3’
[root@jump node20.10]# which python3
/usr/bin/python3
[root@jump node20.10]# rm /usr/bin/python
rm: remove symbolic link ‘/usr/bin/python’? y
[root@jump node20.10]# ln -sv /usr/bin/python3 /usr/bin/python
‘/usr/bin/python’ -> ‘/usr/bin/python3’

6.glibc升级

  • 下载
bash
wget -c -P /opt/tmp/ http://ftp.gnu.org/gnu/glibc/glibc-2.30.tar.gz
wget -c -P /opt/tmp/ http://ftp.gnu.org/gnu/glibc/glibc-2.30.tar.gz
  • 解压
cd /opt/tmp/
tar zxvf glibc-2.30.tar.gz
cd glibc-2.30
mkdir build
cd build
cd /opt/tmp/
tar zxvf glibc-2.30.tar.gz
cd glibc-2.30
mkdir build
cd build
  • 编译
bash
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/opt/rh/devtoolset-9/root/usr/bin

make -j num_cpu

#安装
make install
......
LD_SO=ld-linux-x86-64.so.2 CC="gcc -B/usr/bin/" /usr/bin/perl scripts/test-installation.pl /opt/tmp/glibc-2.30/build/
/usr/bin/ld: cannot find -lnss_test2
collect2: error: ld returned 1 exit status   # 此编译错误可以无视
Execution of gcc -B/usr/bin/ failed!         # 此编译错误可以无视
.....
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:111: install] Error 1
make[1]: Leaving directory '/opt/tmp/glibc-2.30'
make: *** [Makefile:12: install] Error 2    # 此编译错误可以无视
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/opt/rh/devtoolset-9/root/usr/bin

make -j num_cpu

#安装
make install
......
LD_SO=ld-linux-x86-64.so.2 CC="gcc -B/usr/bin/" /usr/bin/perl scripts/test-installation.pl /opt/tmp/glibc-2.30/build/
/usr/bin/ld: cannot find -lnss_test2
collect2: error: ld returned 1 exit status   # 此编译错误可以无视
Execution of gcc -B/usr/bin/ failed!         # 此编译错误可以无视
.....
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:111: install] Error 1
make[1]: Leaving directory '/opt/tmp/glibc-2.30'
make: *** [Makefile:12: install] Error 2    # 此编译错误可以无视

--with-binutils 需要指定gcc命令的路径

  • 检查是否成功
bash
[root@jump node20.10]#  ls -l /lib64/libc.so.6
lrwxrwxrwx 1 root root 12 Jan  9 14:38 /lib64/libc.so.6 -> libc-2.30.so
[root@jump node20.10]#  ls -l /lib64/libc.so.6
lrwxrwxrwx 1 root root 12 Jan  9 14:38 /lib64/libc.so.6 -> libc-2.30.so

❌ 注意

最后需要在执行下面命令,先前安装的会被覆盖

bash
ls -l /usr/lib64/libstdc++.so.6

lrwxrwxrwx 1 root root 19 Jan  9 13:27 /usr/lib64/libstdc++.so.6 -> libstdc++.so.6.0.19

#删除软链
rm /usr/lib64/libstdc++.so.6

#生成新的
ln -sv /data/apps/local/gcc5.5.0/lib64/libstdc++.so.6.0.21 /usr/lib64/libstdc++.so.6


#至此node20.10可以执行
[root@jump node20.10]# ./bin/node --version
v20.10.0
ls -l /usr/lib64/libstdc++.so.6

lrwxrwxrwx 1 root root 19 Jan  9 13:27 /usr/lib64/libstdc++.so.6 -> libstdc++.so.6.0.19

#删除软链
rm /usr/lib64/libstdc++.so.6

#生成新的
ln -sv /data/apps/local/gcc5.5.0/lib64/libstdc++.so.6.0.21 /usr/lib64/libstdc++.so.6


#至此node20.10可以执行
[root@jump node20.10]# ./bin/node --version
v20.10.0