各發行版自帶的 GCC 版本往往偏舊,而新版本編譯器帶來的優化與新標準支持很有吸引力。官方安裝文檔寫得很清楚,但實戰中最容易出問題的環節是依賴庫與 binutils 版本。本文以「源碼安裝」為主線,因為不同系統、不同版本對依賴的要求差異很大,只有按自己機器實際情況配置才靠譜。
GCC 主要通過 SVN 與 FTP 分發,具體版本可查官方發布頁。解壓 tarball 之後,直接在源碼根目錄運行自帶腳本:
[code]./contrib/download_prerequisites[/code]它會自動下載並安裝 GMP、MPFR、MPC 等庫,並建立必要的軟連接。除此之外,系統裡還需要 binutils 提供的
ld、as、size等基礎二進位工具。3. 配置 GCC
兩個關鍵注意事項:
- 不要在源碼目錄裡直接跑
./configure。官方不鼓勵這種做法。正確方式是在源碼同層建一個objdir,進入後執行../srcdir/configure,好處是源碼樹保持乾淨,也方便清理與切換配置。- 若 GCC 需要動態鏈接 GMP、MPFR、MPC,這些庫所在路徑必須在動態庫搜索路徑中,否則配置或編譯階段會報找不到。
4. 編譯與安裝
配置完成後
make、make install即可。強烈建議在配置階段用--prefix指定安裝目錄,後續版本管理和卸載都會輕鬆很多。以 GCC 4.6.2 為例:
[code] tar xzf gcc-4.6.2.tar.gz cd gcc-4.6.2 ./contrib/download_prerequisites cd .. mkdir objdir && cd objdir $PWD/../gcc-4.6.2/configure --prefix=$HOME/gcc-4.6.2 --enable-languages=c,c++,fortran,go make make install5. 常見問題:指定 binutils
如果機器上存在多個 binutils 版本,配置腳本會挑選默認的那個(通常在
/usr/bin)。此時必須顯式指定:[code]./configure --with-as=pathname --with-ld=pathname[/code]6. 實踐案例:CentOS 6.7 源碼裝 GCC 5.3.0
先裝好編譯 GCC 自身所需的 gcc 與 gcc-c++:
[code]yum install gcc gcc-c++[/code]① 安裝 GMP
[code] cd /opt/src wget https://gmplib.org/download/gmp/gmp-6.1.0.tar.bz2 tar xvjf gmp-6.1.0.tar.bz2 mkdir /opt/gmp-6.1.0 && cd gmp-6.1.0 ./configure --prefix=/opt/gmp-6.1.0 make && make check && make install② 安裝 MPFR
[code] cd /opt/src wget https://www.mpfr.org/mpfr-current/mpfr-3.1.4.tar.bz2 tar xvjf mpfr-3.1.4.tar.bz2 mkdir /opt/mpfr-3.1.4 && cd mpfr-3.1.4 ./configure --prefix=/opt/mpfr-3.1.4 --with-gmp=/opt/gmp-6.1.0 make && make check && make install③ 安裝 MPC
[code] cd /opt/src wget https://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz tar xvzf mpc-1.0.3.tar.gz mkdir /opt/mpc-1.0.3 && cd mpc-1.0.3 ./configure --prefix=/opt/mpc-1.0.3 --with-gmp=/opt/gmp-6.1.0 --with-mpfr=/opt/mpfr-3.1.4 make && make check && make install④ 配置動態庫環境
[code] echo /opt/mpc-1.0.3/lib >> /etc/ld.so.conf.d/gcc530-x86_64.conf echo /opt/gmp-6.1.0/lib >> /etc/ld.so.conf.d/gcc530-x86_64.conf echo /opt/mpfr-3.1.4/lib >> /etc/ld.so.conf.d/gcc530-x86_64.conf ldconfig # 確認無報錯⑤ 下載並安裝 GCC 5.3.0
[code] cd /opt/src wget https://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-5.3.0/gcc-5.3.0.tar.bz2 tar xvjf gcc-5.3.0.tar.bz2 mkdir /opt/gcc-5.3.0 && mkdir objdir && cd objdir ../gcc-5.3.0/configure --prefix=/opt/gcc-5.3.0 --enable-languages=c,c++ \ --with-gmp=/opt/gmp-6.1.0 --with-mpfr=/opt/mpfr-3.1.4 --with-mpc=/opt/mpc-1.0.3 \ --disable-multilib make && make install⑥ 切換默認版本:卸載舊版並建立軟連接,同時把新 libstdc++ 加入動態庫路徑。
[code] yum remove gcc gcc-c++ ln -sf /opt/gcc-5.3.0/bin/gcc /usr/bin/ ln -sf /opt/gcc-5.3.0/bin/c++ /usr/bin/ ln -sf /opt/gcc-5.3.0/bin/cpp /usr/bin/ ln -sf /opt/gcc-5.3.0/bin/g++ /usr/bin/ ln -sf /opt/gcc-5.3.0/bin/gcc-ar /usr/bin/ ln -sf /opt/gcc-5.3.0/bin/gcc-nm /usr/bin/ ln -sf /opt/gcc-5.3.0/bin/gcc-ranlib /usr/bin/ ln -sf /opt/gcc-5.3.0/bin/gcov /usr/bin/ ln -sf /opt/gcc-5.3.0/bin/gcov-tool /usr/bin/ ln -sf /opt/gcc-5.3.0/bin/gcc /usr/bin/cc ln -sf /opt/gcc-5.3.0/lib64/libstdc++.so.6.0.21 /usr/lib64/libstdc++.so.6 echo /opt/gcc-5.3.0/lib/../lib64 >> /etc/ld.so.conf.d/gcc-530_x86_64.conf ldconfig # 確認無報錯完成後用
gcc --version驗證即可。整個過程最耗時的是 make,建議開多核並聯編譯,同時留意磁盤空間。