2018-01-17

昨天在 VMware 14 上裝了 CentOS,準備用來學習 C 語言,然后要是使用

yum  groupinstall Development tools

安裝整組開發工具 (包含 gcc) 的話,gcc 的版本太低了,所以就選擇下載最新版的 gcc 源碼編譯安裝。

編譯安裝 gcc ,要求要有 gcc 編譯器,所以上面的命令在你沒有安裝 gcc 時是必須的, 否則會報錯說沒有 gcc。

注: 如果權限不夠,就要在命令前面 sudo 或者 切換到 root 用戶

1. 下載源碼

wget https://mirrors.ustc.edu.cn/gnu/gcc/gcc-7.2.0/gcc-7.2.0.tar.xz

2. 解壓源碼

tar xvf gcc-7.2.0.tar.xz

cd gcc-7.2.0 (進入目錄后,才可以添加依賴)

3. 添加依賴

./contrib/download_prerequisites

注: 包含(gmp,mpfr,mpc)

4. 編譯安裝

mkdir gccbuild
cd gccbuild
../configure --prefix=/home/oldbiwang/gcc/gcc-7.2.0/ --enable-
checking=release --enable-languages=c,c++ --disable-multilib

其中 --prefix=/home/oldbiwang/gcc/gcc-7.2.0/ 指定安裝路徑, --enable-checking=release 檢查內部錯誤, 我不寫的時候是編譯錯誤的。

--enable-checking=release

When you specify this option, the compiler is built to perform internal consistency checks of the requested complexity. This does not change the generated code, but adds error checking within the compiler. This will slow down the compiler and may only work properly if you are building the compiler with GCC. (release’ (cheapest checks ‘assert,runtime’) )

--enable-languages=c,c++ 指定支持的語言, --enable-checking=release 選項

that multiple target libraries to support different target variants, calling conventions, etc. should not be built. The default is to build a predefined set of them.

多個目標庫為了支持不同的目標變量,調用約定等等。胡亂翻譯一下。不提倡創建,所以 disabled。

接下來是安裝了

make -j4
sudo make install

make -j4 這個命令跑了一個鐘吧,可能是我的電腦太渣了,編譯了好久。

5. 添加路徑到 PATH

5.1 這種是一次性配置,關機重啟之后就得重新配置

export PATH=/usr/local/gcc-7.2.0/bin:$PATH

5.2 不建議上面的配置,我們可以編輯 ~/.bashrc, 加上上面一行 export PATH=/usr/local/gcc-7.2.0/bin:$PATH

sudo vi ~/.bashrc

總結: gcc 到這里就安裝完畢了, 輸入 gcc -version

[oldbiwang@192 etc]$ gcc --version                                                                             
gcc (GCC) 7.2.0                                                                                                
Copyright ? 2017 Free Software Foundation, Inc.                                                                                                                                                 
本程序是自由軟件;請參看源代碼的版權聲明。本軟件沒有任何擔保;                                                                                                                                  
包括沒有適銷性和某一專用目的下的適用性擔保。

參考文章
linuxgcc
installgcc

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容