Linux環境安裝GCC

安裝GCC-4.6.1詳細教程  


一般情況下只需要插入系統盤,把相關的包裝上即可,如果不方便用系統盤的情況下再用如下方法


安裝方法運行環境:VMware? Workstation 7.1.2 build-301548
Linux版本:Red Hat Enterprise Linux 5.5
kernel版本:Linux kernel 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:43 EDT 2010 i686 i686 i386 GNU/Linux
注意:因爲wordpress轉義的問題:可能會導致下面的--翻譯成—,大家在試驗的時候不要複製上面的代碼
自己手動敲寫,比如prefix,with-gmp,with-mpfr,with-mpc前都是兩個 - 
1.下載gcc-4.6.1安裝包
gcc各版本瀏覽地址:http://ftp.gnu.org/gnu/gcc/
gcc-4.6.1瀏覽地址:http://ftp.gnu.org/gnu/gcc/gcc-4.6.1/
gcc-4.6.1下載地址:http://ftp.gnu.org/gnu/gcc/gcc-4.6.1/gcc-4.6.1.tar.bz2
2.將gcc-4.6.1.tar.bz2放到/opt文件夾下解壓
[[email protected] ~]# cd /opt
[[email protected] opt]# tar xjvf gcc-4.6.1.tar.bz2
3.創建安裝目錄
[[email protected] opt]# mkdir /usr/local/gcc-4.6.1/
4.進入安裝目錄
[[email protected] opt]# cd /usr/local/gcc-4.6.1/
5.配置安裝文件
[[email protected] gcc-4.6.1]# /opt/gcc-4.6.1/configure --prefix=/usr/local/gcc-4.6.1
(執行源目錄 /opt/gcc-4.6.1/中的configure命令,配置將gcc安裝到目標目錄/usr/local/gcc-4.6.1/下,這裏–prefix選項代表要將該庫安裝在哪裏,我是裝在/usr/local/gcc-4.6.1目錄下,後面的安裝都會用到這個選項)


如果執行這步的時候出現瞭如下錯誤:
----------------------------------------------------------------------------------
[[email protected] gcc-4.6.1]# /opt/gcc-4.6.1/configure --prefix=/usr/local/gcc-4.6.1/
checking build system type… i686-pc-linux-gnu
checking host system type… i686-pc-linux-gnu
checking target system type… i686-pc-linux-gnu
checking for a BSD-compatible install… /usr/bin/install -c
checking whether ln works… yes
checking whether ln -s works… yes
checking for a sed that does not truncate output… /bin/sed
checking for gawk… gawk
checking for gcc… no
checking for cc… no
checking for cl.exe… no
configure: error: in `/usr/local/gcc-4.6.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.
----------------------------------------------------------------------------------
上面錯誤說明$PATH裏沒有找到C的編譯器
如果系統有gcc編譯器,那就設置下環境變量
比如:
export PATH=$PATH:/usr/local/bin
如果沒有找到那麼先安裝gcc低版本的rpm包
插入linux軟件盤(一般在第一張或第二張),掛載光盤,按下面順序依次安裝:


[[email protected] opt]# rpm -ivh cpp-4.1.2-48.el5.i386.rpm
[[email protected] opt]# rpm -ivh kernel-headers-2.6.18-194.el5.i386.rpm
[[email protected] opt]# rpm -ivh glibc-headers-2.5-49.i386.rpm
[[email protected] opt]# rpm -ivh glibc-devel-2.5-49.i386.rpm
[[email protected] opt]# rpm -ivh libgomp-4.4.0-6.el5.i386.rpm
[[email protected] opt]# rpm -ivh gcc-4.1.2-48.el5.i386.rpm


重複步驟5,又有新錯誤了,下面是部分錯誤代碼 !如果沒有錯誤,直接跳到步驟7 !
----------------------------------------------------------------------------------
configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations.
----------------------------------------------------------------------------------


錯誤說明要安裝gcc需要GMP、MPFR、MPC這三個庫,可從ftp://gcc.gnu.org/pub/gcc/infrastructure/下載相應的壓縮包。由於MPFR依賴GMP,而MPC依賴GMP和MPFR,所以要先安裝GMP,其次MPFR,最後纔是MPC。這裏三個庫我用的版本分別是gmp4.3.2,mpfr2.4.2和mpc0.8.1,都放在 /opt文件夾下。
①.安裝gmp4.3.2
[[email protected] opt]# tar jxvf gmp-4.3.2.tar.bz2
[[email protected] opt]# mkdir /usr/local/gmp-4.3.2
[[email protected] opt]# cd /usr/local/gmp-4.3.2
[[email protected] gmp-4.3.2]# /opt/gmp-4.3.2/configure --prefix=/usr/local/gmp-4.3.2
[[email protected] gmp-4.3.2]# make (編譯)
[[email protected] gmp-4.3.2]# make install (執行安裝)
②安裝mpfr2.4.2
[[email protected] opt]# tar jxvf mpfr2.4.2.tar.bz2
[[email protected] opt]# mkdir /usr/local/mpfr-2.4.2
[[email protected] opt]# cd /usr/local/mpfr-2.4.2
[[email protected] mpfr-2.4.2]# /opt/mpfr-2.4.2/configure --prefix=/usr/local/mpfr-2.4.2 --with-gmp=/usr/local/gmp-4.3.2
(注意配置的時候要把依賴關係選項加進去)
[[email protected] mpfr-2.4.2]# make
[[email protected] mpfr-2.4.2]# make install
③安裝mpc0.8.1
[[email protected] opt]# tar jxvf gmpc0.8.1.tar.bz2
[[email protected] opt]# mkdir /usr/local/mpc-0.8.1
[[email protected] opt]# cd /usr/local/mpc-0.8.1
[[email protected] mpc-0.8.1]# /opt/mpc-0.8.1/configure --prefix=/usr/local/mpc-0.8.1 --with-gmp=/usr/local/gmp-4.3.2 --with-mpfr=/usr/local/mpfr-2.4.2
[[email protected] mpc-0.8.1]# make
[[email protected] mpc-0.8.1]# make install
④再次安裝GCC 配置安裝選項
[[email protected] mpc-0.8.1]# cd /usr/local/gcc-4.6.1
[[email protected] gcc-4.6.1]# /opt/gcc-4.6.1/configure --prefix=/usr/local/gcc-4.6.1 -enable-threads=posix -disable-checking -disable-multilib -enable-languages=c,c++ --with-gmp=/usr/local/gmp-4.3.2 --with-mpfr=/usr/local/mpfr-2.4.2 --with-mpc=/usr/local/mpc-0.8.1


6.編譯安裝文件
[[email protected] gcc-4.6.1]# make
大概需要1個小時左右,喝杯coffee,慢慢等待吧!
一個小時後···
第二個錯誤出現了:


打開/usr/local/gcc-4.6.1/i686-pc-linux-gnu/libgcc/config.log
ctrl+f查找error 發現如下錯誤
/----------------------------------------------------------------------------------
/usr/local/gcc-4.6.1/./gcc/cc1: error while loading shared libraries: libmpc.so.2: cannot open shared object file: No such file or directory
configure:3058: $? = 1
----------------------------------------------------------------------------------/
在網上找到了解決方法,需要添加環境變量LD_LIBRARY_PATH以指出前面三個庫的位置,鍵入以下命令:
[[email protected] gcc-4.6.1]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc-0.8.1/lib:/usr/local/gmp-4.3.2/lib:/usr/local/mpfr-2.4.2/lib
再次執行步驟6→
大概三小時後···
終於編譯成功了,效果圖入下:




7.執行安裝gcc
[[email protected] gcc-4.6.1]# make install
[[email protected] gcc-4.6.1]# 
如果不出意外,那麼到現在應該安裝成功了,不過還是不能使用新版本的gcc,因爲新版本的gcc還沒有加入命令搜索路徑中!
8.建立軟鏈接
[[email protected] /]# sudo ln -s /usr/local/gcc-4.6.1/bin/gcc gcc461
[[email protected] /]# sudo ln -s /usr/local/gcc-4.6.1/bin/g++ g++461
9.添加環境變量 
打開/etc目錄下的bashrc文件添加如下代碼:
LD_LIBRARY_PATH=:/usr/local/mpc-0.8.1/lib:/usr/local/gmp-4.3.2/lib:/usr/local/mpfr-2.4.2/lib:/usr/local/gcc-4.6.1/lib


export LD_LIBRARY_PATH


如下兩個命令就可把gcc,gcc-c++統統安裝上
       yum -y install gcc
      yum -y install gcc-c++

發佈了28 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章