CentOS7升级gcc 和gdb

升级后版本: gcc-5.4.0 gdb-7.11.1

安装开发必备环境

yum groupinstall "Development Tools"
yum install glibc-static libstdc++-static

编译安装gcc-5.4.0

gcc下载地址

tar -xvf gcc-5.4.0.tar.bz2
cd gcc-5.4.0
./contrib/download_prerequisits
mkdir build
cd build
../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
make(建议不要使用make -j来编译,虽然可以缩短编译时间,但极大可能会编译失败)
make install

其中执行./contrib/download_prerequisits将自动下载以下几个文件,这个几个文件在gcc编译时需要: - mpfr-2.4.2.tar.bz2 - gmp-4.3.2.tar.bz2 - mpc-0.8.1.tar.gz - isl-0.15.tar.bz2

make install 时, 自动安装到/usr/local/gcc-5.40

解决运行程序时, gcc 报错’GLIBCXX_3.4.21’ not found

这是因为升级gcc时,生成的动态库没有替换老版本gcc的动态库导致的,将gcc最新版本的动态库替换系统中老版本的动态库即可解决,运行以下命令检查动态库: strings /lib64/libstdc++.so.6 | grep GLIBC

以下是输出结果:

GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_3.4.18
GLIBCXX_3.4.19
GLIBC_2.3
GLIBC_2.2.5
GLIBC_2.14
GLIBC_2.4
GLIBC_2.3.2
GLIBCXX_DEBUG_MESSAGE_LENGTH

从输出结果可以看到并没有GLIBCXX_3.4.21,所以可以断定我们的程序运行时动态加载的是老的动态库,解决这个问题需要将当前链接文件的链接指向改成最新的动态库地址:

cp /usr/local/lib64/libstdc++.so.6.0.21 /lib64
cd /lib64
rm -rf libstdc++.so.6
ln -s libstdc++.so.6.0.21 libstdc++.so.6

然后你可以执行以下命令来查看GLIBCXX_3.4.21已经可以找到了:

strings /lib64/libstdc++.so.6 | grep GLIBC

解决了这个问题终于可以执行程序了,然后又测试了-g选项来编译程序,编译好程序调试程序时并不能够设置断点以及print变量的值,gdb调试中出现:

Missing separate debuginfos, use: debuginfo-install glibc-2.17-106.e17\_2.6.x86\_4 libgcc-4.8.5-4.e17.x86_64 的问题,通过上网查阅资料,是因为gcc版本和gdb版本并不匹配,或者说gdb版本过低

编译安装gdb-7.11.1

gdb下载地址

tar -xvf gdb-7.11.1.tar.gz
cd gdb-7.11.1
./configure
make
make install

当执行 make install 时gdb安装出现了错误:

WARNING: 'makeinfo' is missing on your sysem

则需安装相关依赖程序:

yum install texinfo libncurses5-dev

如果调试程序时出现下面信息时:

warning: File "/usr/local/lib64/libstdc++.so.6.0.21-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
    add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.21-gdb.py
line to your configuration file "/root/.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "/root/.gdbinit".

解决方法: 将以下信息放入~/.gdbinit

add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.21-gdb.py
set auto-load safe-path /

若想通过gdb来调试STL容器,则还需要做一些配置,可以通过GDB Python pretty printers来解决这个问题:

svn checkout svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python stlPrettyPrinter
mv stlPrettyPrinter /usr/local

然后将下面的配置信息放入~/.gdbinit

python
import sys
sys.path.insert(0, '/usr/local/stlPrettyPrinter')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计