1. 程式人生 > >ubuntu18.04安裝gcc詳細步驟(附問題集)

ubuntu18.04安裝gcc詳細步驟(附問題集)

首先是下載gcc包,可以在GCC的官方網站http://gcc.gnu.org/ 下載到各個版本。 目前最高版本是gcc-8.2.0。

一、在安裝gcc前,需要先安裝 MPFR 、GMP 和MPC

GCC編譯需要mpfr和mpc(-->gmp、-->mpfr)庫的支援,依次安裝這幾個庫,其中mpfr可直接安裝,安裝mpc依賴mpfr和gmp庫(對版本有要求,建議安裝最新版本),然後執行configure時指定mpfr和gmp的include、lib路徑。

安裝過程是先安裝 gmp,然後再安裝 mpfr.和mpc,最後安裝gcc

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

4個安裝包需全部複製到/usr/locar/下:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

  1.    安裝gmp

首先將安裝包(gmp-6.1.2)複製到/usr/local下

注:安裝均在root許可權下進行:

[[email protected] ~]$ su –
Password:
[[email protected]
]# cd /usr/local [[email protected]]# cp /mnt/hgfs/Ubuntu-tool/gcc-tool/gmp6.1.2.tar.bz2/ ./ -r [[email protected]]# tar jxvf gmp-6.1.2.tar.bz2 [[email protected]]# cd gmp-6.1.2 [[email protected] nandycool-virtual-machine]# ./configure [[email protected] nandycool-virtual-machine]# make [
[email protected]
nandycool-virtual-machine]# make check

這一步是必要的,用來檢視有沒有檔案不匹配或缺失,在最後提示,到時注意看有沒有 Error 之類的提示,如果沒有則說明安裝正常.

[[email protected] nandycool-virtual-machine]# make install

安裝編譯後的軟體,完成了安裝過程

有必要說明程式的標頭檔案(.h)和庫檔案(lib*)的位置, 本次安裝會安裝在  /usr/local/include 和/usr/local/lib   這個對後面安裝 mpfr 很有作用.(安裝完成後會自動在當前目錄生成include和lib檔案)

注意:如若出現以下問題:

①安裝gmp遇到 configure:error:no usable m4 in$path or /user/5bin解決方案

sudo apt-get install m4     安裝m4即可

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2. 安裝 mpfr

首先將安裝包(mpfr-4.0.1.tar.gz)複製到/usr/local下

[[email protected]]# cp /mnt/hgfs/Ubuntu-tool/gcc-tool/ mpfr-4.0.1.tar.gz/ ./ -r
[[email protected] nandycool-virtual-machine]# tar zxvf mpfr-4.0.1.tar.gz
[[email protected] nandycool-virtual-machine]# cd mpfr-4.0.1
[[email protected] nandycool-virtual-machine]# ./configure
[[email protected] nandycool-virtual-machine]# make
[[email protected] nandycool-virtual-machine]# make check

如果出現如下資訊說明編譯成功了.

接下來安裝

[[email protected] nandycool-virtual-machine]# make install

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3、  安裝 MPC

首先將安裝包(mpc-1.1.0.tar.gz)複製到/usr/local下

[[email protected]]# cp /mnt/hgfs/Ubuntu-tool/gcc-tool/mpc-1.1.0.tar.gz / ./ -r
[[email protected] nandycool-virtual-machine]# tar zxvf mpc-1.1.0.tar.gz
[[email protected] nandycool-virtual-machine]# cd mpc-1.1.0

//[[email protected] nandycool-virtual-machine]# vi ./src/acos.c +192
//#: rnd_im == MPFR_RNDD ? MPFR_RNDU
//註釋掉第192行(# :rnd_im == GMP_RNDA ? GMP_RNDZ)即在該行前面加“#”

[[email protected] nandycool-virtual-machine]# ./configure --with-gmp-include=/usr/local/include --with-gmp-lib=/usr/local/lib --with-mpfr-include=/usr/local/include    --with-mpfr-lib=/usr/local/lib
[[email protected] nandycool-virtual-machine]# make
[[email protected] nandycool-virtual-machine]# make check (這個時候可能會有錯誤,但是這個沒有關係,可以跳過!)
[[email protected] nandycool-virtual-machine]# make install

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

二、安裝gcc 8.2.0

1、配置gmp和mpfr的庫檔案路徑, 在.bash_profile(一般在/root 目錄下)配置 LD_LIBRARY_PATH,把 GMP 與 MPFR對應的 lib 配置上.

[[email protected] nandycool-virtual-machine]# cd
[[email protected] nandycool-virtual-machine]# vi .bash_profile  
export C_INCLUDE_PATH=/usr/local/include:$C_INCLUDE_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

按esc,輸入 :wq 儲存退出。

重啟terminal生效或者使用  source   /root/.bash_profile

[[email protected] nandycool-virtual-machine]# source .bash_profile

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

2. 安裝gcc

[[email protected]]# cp /mnt/hgfs/Ubuntu-tool/gcc-tool/gcc-8.2.0.tar.gz / ./ -r
[[email protected] nandycool-virtual-machine]# tar zxvf gcc-8.2.0.tar.gz
[[email protected] nandycool-virtual-machine]# cd gcc-8.2.0
[[email protected] nandycool-virtual-machine]# mkdir gcc_temp
[[email protected] nandycool-virtual-machine]# cd gcc_temp
[[email protected] nandycool-virtual-machine]# ../configure --prefix=/usr   --with-gmp=/usr/local --with-mpfr=/usr/local --with-mpc=/usr/local    (這一步可能會出現問題)
[[email protected] nandycool-virtual-machine]# make          (我編譯了兩個半小時)
[[email protected] nandycool-virtual-machine]# make install

大功告成,over

編譯成功後截圖:

檢視gcc版本:

[[email protected] nandycool-virtual-machine]#gcc -v

如圖:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

如若出現以下錯誤:

解決辦法:輸入以下命令編譯:

 /usr/local/gcc-8.2.0/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib

編譯完後就可以接著下一步驟就能成功安裝gcc了!!