一、下載所需依賴
# 1、下載gcc的依賴包 $ wget ftp://mirror.nl.leaseweb.net/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz # 2、下載libmpc依賴包 $ wget ftp://mirror.nl.leaseweb.net/gnu/mpc/mpc-1.0.3.tar.gz # 3、下載mpfr依賴包 $ wget http://ftp.gnu.org/gnu/mpfr/mpfr-3.1.4.tar.gz
在Linux上安裝GCC需要許多的依賴包。在離線環境下我們需要先下載好並安裝這些依賴包,才能安裝GCC。GCC的依賴包包括:GMP、MPFR、MPC。以下提供了這些依賴包的下載鏈接。
二、安裝依賴
# 1、解壓gcc $ tar -xvf gcc-7.3.0.tar.gz # 2、解壓libmpc $ tar -xvf mpc-1.0.3.tar.gz # 3、解壓mpfr $ tar -xvf mpfr-3.1.4.tar.gz # 4、為了保證穩定,需要將三個包的安裝路徑都設定好(都裝到同一個目錄下面就行) $ export PREFIX=/usr/local/gcc-7.3.0 # 5、安裝gmp $ cd gcc-7.3.0 $ cd contrib/download_prerequisites $ ./download_prerequisites $ cd .. $ mkdir build-gmp $ cd build-gmp/ $ ../gmp-6.1.0/configure --prefix=$PREFIX $ make && make install # 6、安裝mpfr $ cd .. $ mkdir build-mpfr $ cd build-mpfr $ ../mpfr-3.1.4/configure --prefix=$PREFIX --with-gmp=$PREFIX $ make && make install # 7、安裝mpc $ cd .. $ mkdir build-mpc $ cd build-mpc $ ../mpc-1.0.3/configure --prefix=$PREFIX --with-gmp=$PREFIX --with-mpfr=$PREFIX $ make && make install
這些依賴的安裝過程與一般的依賴安裝過程一樣。我們需要先解壓、configure,然後進行make、make install,安裝到指定的位置。
三、安裝GCC
# 1、修改GCC安裝腳本的文件名 $ cd /usr/local $ cp gcc-7.3.0.tar.gz /opt $ cd /opt $ tar -zxvf gcc-7.3.0.tar.gz $ cd gcc-7.3.0 # 2、用動態鏈接的方式編譯GCC $ ./contrib/download_prerequisites $ mkdir ../gcc-7.3.0-build $ cd ../gcc-7.3.0-build $ ../gcc-7.3.0/configure --prefix=/usr/local/gcc-7.3.0 --with-system-zlib --enable-checking=release --enable-languages=c,c++ --disable-multilib $ make
在安裝好依賴包之後,我們就可以開始安裝GCC了。需要注意的一點是,在安裝時需要單獨安裝一個叫做zlib的壓縮庫,以便在編譯GCC時使用。具體的安裝步驟如下。
四、測試GCC是否安裝成功
# 1、測試新GCC編譯hello.c $ cd /opt/gcc-7.3.0-build $ echo "#include int main(){ printf("Hello World!\n"); return 0; }" > hello.c $ ./gcc/xgcc -B./gcc -B/usr/local/gcc-7.3.0/x86_64-pc-linux-gnu/bin/ -B/usr/local/gcc-7.3.0/x86_64-pc-linux-gnu/lib/ -isystem /usr/local/gcc-7.3.0/x86_64-pc-linux-gnu/include -isystem /usr/local/gcc-7.3.0/x86_64-pc-linux-gnu/sys-include -o hello.o -c hello.c $ ./gcc/xgcc -B./gcc -B/usr/local/gcc-7.3.0/x86_64-pc-linux-gnu/bin/ -B/usr/local/gcc-7.3.0/x86_64-pc-linux-gnu/lib/ -isystem /usr/local/gcc-7.3.0/x86_64-pc-linux-gnu/include -isystem /usr/local/gcc-7.3.0/x86_64-pc-linux-gnu/sys-include -o hello hello.o $ ./hello Hello World! # 2、測試新GCC是否可以編譯內核 $ cd /usr/src/linux $ make CC=/usr/local/gcc-7.3.0/bin/gcc
在安裝GCC之後,我們需要進行測試,確保GCC能夠正常編譯。我們可以編寫一個小程序檢測是否能夠編譯,並且測試是否可以編譯內核。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/257839.html