1. 程式人生 > >CentOS7 升級gcc和gdb

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

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

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