一、什么是Gcc
Linux系統下的Gcc(GNU C Compiler)是GNU推出的功能強大、性能優越的多平台編譯器,是GNU的代表作品之一。gcc是可以在多種硬體平台上編譯出可執行程序的超級編譯器,其執行效率與一般的編譯器相比平均效率要高20%~30%。
Gcc編譯器能將C、C++語言源程序、匯程式化序和目標程序編譯、連接成可執行文件,如果沒有給出可執行文件的名字,gcc將生成一個名為a.out的文件。在Linux系統中,可執行文件沒有統一的后綴,系統從文件的屬性來區分可執行文件和不可執行文件。
.C或.cc或.cxx為后綴的文件,是C++源代碼文件;
-Idirname,將dirname所指出的目錄加入到程序頭文件目錄列表中,是在預編譯過程中使用的參數。六、Gcc的安裝方法運行環境: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文件夾下解壓
[root@rekfan.com ~]# cd /opt
[root@rekfan.com opt]# tar xjvf gcc-4.6.1.tar.bz2
3.創建安裝目錄
[root@rekfan.com opt]# mkdir /usr/local/gcc-4.6.1/
4.進入安裝目錄
[root@rekfan.com opt]# cd /usr/local/gcc-4.6.1/
5.配置安裝文件
[root@rekfan.com 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目錄下,后面的安裝都會用到這個選項)
如果執行這步的時候出現了如下錯誤:
----------------------------------------------------------------------------------
[root@rekfan.com 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軟件盤(一般在第一張或第二張),掛載光盤,按下面順序依次安裝:
[root@rekfan.com opt]# rpm -ivh cpp-4.1.2-48.el5.i386.rpm
[root@rekfan.com opt]# rpm -ivh kernel-headers-2.6.18-194.el5.i386.rpm
[root@rekfan.com opt]# rpm -ivh glibc-headers-2.5-49.i386.rpm
[root@rekfan.com opt]# rpm -ivh glibc-devel-2.5-49.i386.rpm
[root@rekfan.com opt]# rpm -ivh libgomp-4.4.0-6.el5.i386.rpm
[root@rekfan.com 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
[root@rekfan.com opt]# tar jxvf gmp-4.3.2.tar.bz2
[root@rekfan.com opt]# mkdir /usr/local/gmp-4.3.2
[root@rekfan.com opt]# cd /usr/local/gmp-4.3.2
[root@rekfan.com gmp-4.3.2]# /opt/gmp-4.3.2/configure --prefix=/usr/local/gmp-4.3.2
[root@rekfan.com gmp-4.3.2]# make (編譯)
[root@rekfan.com gmp-4.3.2]# make install (執行安裝)
②安裝mpfr2.4.2
[root@rekfan.com opt]# tar jxvf mpfr2.4.2.tar.bz2
[root@rekfan.com opt]# mkdir /usr/local/mpfr-2.4.2
[root@rekfan.com opt]# cd /usr/local/mpfr-2.4.2
[root@rekfan.com 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
(注意配置的時候要把依賴關系選項加進去)
[root@rekfan.com mpfr-2.4.2]# make
[root@rekfan.com mpfr-2.4.2]# make install
③安裝mpc0.8.1
[root@rekfan.com opt]# tar jxvf gmpc0.8.1.tar.bz2
[root@rekfan.com opt]# mkdir /usr/local/mpc-0.8.1
[root@rekfan.com opt]# cd /usr/local/mpc-0.8.1
[root@rekfan.com 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
[root@rekfan.com mpc-0.8.1]# make
[root@rekfan.com mpc-0.8.1]# make install
④再次安裝GCC 配置安裝選項
[root@rekfan.com mpc-0.8.1]# cd /usr/local/gcc-4.6.1
[root@rekfan.com 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.編譯安裝文件
[root@rekfan.com 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以指出前面三個庫的位置,鍵入以下命令:
[root@rekfan.com 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
[root@rekfan.com gcc-4.6.1]# make install
[root@rekfan.com gcc-4.6.1]#
如果不出意外,那么到現在應該安裝成功了,不過還是不能使用新版本的gcc,因為新版本的gcc還沒有加入命令搜索路徑中!
8.建立軟鏈接
[root@rekfan.com /]# sudo ln -s /usr/local/gcc-4.6.1/bin/gcc gcc461
[root@rekfan.com /]# 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