1. GCC
這篇文章主要用來提供一些常見的指導,通常我們在安裝GCC過程中都會出現常見的問題,官方的安裝文件可以訪問官方網址: Installing GCC
我們主要從 “源文件來安裝GCC” 因爲不同的操作系統不同的版本已經GCC所需要的必要的軟件的版本不同,我們通常都是在下載完源代碼文件後根據自身電腦的配置來完成編譯和安裝。
2. 下載GCC源代碼
GCC主要通過 SVN 和 FTP 傳播,可以參考官方的 版本發佈 獲取最新的GCC源代碼,下載完成後,我們需要首先確認GCC安裝時必須的所有工具,比如 GMP,MPFR,MPC庫文件和一些必要的Binutils (比如ld,as,size等必要的二進制工具)
下載完成後,解壓下載的tarball文件,在解壓目錄下,直接運行 一個 contrib/download_prerequisties 的腳本文件,該腳本文件會自動運行檢測並安裝所需的庫文件(添加必要的軟連接soft link到必要的目錄)
3. 配置 GCC
很多人都急於進入到解壓後的文件直接配置必須的安裝條件,在沒有充分閱讀官方的文檔之前很容易犯下這樣的錯誤,
1. 不要在srcdir 源文件目錄下運行 ./configure 這是不鼓勵的,我們需要在源文件之外運行它,可以在srcdir同層目錄創建一個objdir,然後進入objdir敲擊 ../srcdir/configure 進行必要的配置
2. GCC如果需要動態鏈接 GMP,MPFR,MPC庫,則相應的庫文件必須要得包含在 動態庫尋找路徑中。
3. 編譯和安裝GCC
在配置完之後,直接運行make, make install 會編譯GCC和安裝GCC, 但通常我們建議在之前的配置階段 可以指定prefix 目錄,這樣可以方便之後更好的管理
4. 舉個例子,下載 4.6.2版本的gcc 源代碼文件
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 install
5. FAQ 常見問題 (指定binutils)
1. 配置過程中,我們系統中如果存在多個 binutils 的版本,我們需要指定所在的ld, as 路徑,否則配置就會選擇默認的binutils,默認的碧奴體力上安裝在 /usr/bin目錄下,假如最新的binutils安裝在 /opt/usr/binutils_2.26.8/bin目錄下,則應該指定binutils的安裝目錄
./configure --with-as=pathname --with-ld=pathname
6. 實踐案例
In order to compile gcc and gcc-c++ from source code,you need the following two modules.
#yum install gcc
#yum install gcc-c++
1.Install GMP 4.2+,
#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
2.Install MPFR 2.4.0+
#cd /opt/src #wget http://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
3.Install MPC 0.8.0+
#cd /opt/src #wget http://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
4.Configure environmental settings
#echo /opt/mpc-1.0.3/lib >> /etc/ld.so.conf.d/gcc530-x86_64.conf #echo /opt/gmp-6.1.10/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 *Make sure there is no error/
5.Download gcc and install gcc
#cd /opt/src/ #wget http://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 #yum remove gcc #yum remove 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 Make sure there is no error.