ubuntu18.04 安裝gcc 7.4


轉載:https://blog.csdn.net/weixin_44187892/article/details/105968534

Ubuntu18.04安裝gcc-7.4.0

        一、安裝依賴包
            獲取安裝包並解壓
            安裝依賴包
        二、安裝gcc
        三、驗證gcc
            參考文獻

一、安裝依賴包
獲取安裝包並解壓

gcc-7.4.0下載
http://mirrors.nju.edu.cn/gnu/gcc/
分別下載gmp6.1.0、mpfr3.1.4、mpc1.0.3
http://mirror.linux-ia64.org/gnu/gcc/infrastructure/
注意:gpm、mpfr、mpc選擇最新版本即可,我這里下載的是gmp6.1.0、mp3.1.4、mpc1.0.3
分別解壓gcc-7.4.0、gmp6.1.0、mp3.1.4、mpc1.0.3
tar -xzvf gcc-7.4.0.tar.gz
tar -xjvf gmp-6.1.0.tar.bz2
tar -xjvf mpfr-3.1.4.tar.bz2
tar -xzvf mpc-1.0.3.tar.gz
gcc放入home目錄下(其他位置也可以),將解壓的gmp6.1.0、mp3.1.4、mpc1.0.3文件夾放入gcc-7.4.0文件夾下
安裝依賴包

打開終端CTRL+ALT+T,輸入以下命令進行配置:

$cd gcc-7.4.0\

 
在gcc7.4.0目錄下新建文件temp,進入temp

$cd temp\



接下來安裝gmp、mpfr、mpc(安裝順序只能是先安裝gmp,再安裝mpfr,最后安裝mpc)

$sudo apt-get install m4
$../gmp-6.1.0/configure --prefix=/usr/local/gmp-6.1.0
$make
$sudo make install


注意:如果報錯:configure: error: could not find a working compiler
輸入sudo apt-get install build-essential即可解決
清除temp文件夾里面所有文件,只保留temp空文件夾
然后安裝mpfr和mpc的方法和gmp類似

#mpfr安裝
$../mpfr-3.1.4/configure --prefix=/usr/local/mpfr-3.1.4 --with-gmp=/usr/local/gmp-6.1.0
$make
$sudo make install



和上面一樣清除temp文件夾

#mpc安裝
$../mpc-1.0.3/configure --prefix=/usr/local/mpc-1.0.3 --with-gmp=/usr/local/gmp-6.1.0 --with-mpfr=/usr/local/mpfr-3.1.4
$make
$sudo make install



鏈接3個lib

$export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc-1.0.3/lib:/usr/local/gmp-6.1.0/lib:/usr/local/mpfr-3.1.4/lib 


二、安裝gcc

再次清除temp文件夾,輸入指令進入temp文件夾里,安裝gcc。

#安裝gcc
$../../gcc-7.4.0/configure --prefix=/usr/local/gcc-7.4 -enable-threads=posix -disable-checking -disable-multilib -enable-languages=c,c++,java,objc -with-gmp=/usr/local/gmp-6.1.0 -with-mpfr=/usr/local/mpfr-3.1.4 -with-mpc=/usr/local/mpc-1.0.3
$make
$sudo make install

   

注意1:可能出現錯誤(參考https://blog.csdn.net/Tsuki_jhsyi77/article/details/103111687)

checking for isl 0.15 or later... no required isl version is 0.15 or later
    解決方法:
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2     
tar -jxvf isl-0.18.tar.bz2 cd isl-0.18/ mkdir temp cd temp sudo
../configure --prefix=/usr/local/isl-0.18 sudo make sudo make install

注意1.1:可能出現錯誤:

configure: error: gmp.h header not found

解決方法:

sudo apt-get install  libgmp3-dev

sudo ../configure --prefix=/usr/local/gcc-9.2.0 --enable-threads=posix --disable-checking --enable--long-long --with-gmp=/usr/local/gmp-6.1.2 --with-mpfr=/usr/local/mpfr-4.0.2 --with-mpc=/usr/local/mpc-1.1.0 --with-isl=/usr/local/isl-0.18 --disable-multilib

 


注意2:在gcc安裝過程中,可能出現錯誤
configure: error:
The following requested languages could not be built: java
Supported languages are: c,brig,c,c++,d,fortran,go,lto,objc,obj-c++
解決辦法:直接把jave拿掉,再次運行。
error while loading shared libraries: libmpfr.so.4: cannot open shared object file: No such file or directory
Makefile:1933: recipe for target ‘s-selftest’ failed
解決辦法:
sudo ln -s /usr/lib/x86_64-linux-gnu/libmpfr.so.6 /usr/lib/x86_64-linux-gnu/libmpfr.so.4
如果出現configure: error: in /usr/local/src/gcc-7.4.0/build/gcc': configure: error: C++ preprocessor "/lib/cpp" fails sanity check Seeconfig.log’ for more details.
解決方法: apt-get install build-essential
apt-get install g++

更多錯誤參考:
https://blog.csdn.net/sslj81/article/details/102014787

最后將安裝gcc-7.4.0鏈接到/usr/bin/gcc中

$cd /usr/bin
$sudo ln -s /usr/local/gcc-7.4/bin/gcc gcc7.4
$sudo ln -s /usr/local/gcc-7.4/bin/g++ g++7.4


安裝完成
三、驗證gcc

切換到剛剛安裝的gcc版本

查看當前安裝所有的gcc版本

$ls /usr/bin/gcc*


查看當前版本

$gcc --version


設置需要的版本

$sudo apt-get install gcc-7.4 gcc-7.4-multilib g++-7.4 g++-7.4-multilib
$sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7.4 40
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7.4 40

 

切換到剛剛安裝版本

$sudo update-alternatives --config gcc


出現pressto keep the current choice[*],or type selection number:
輸入對應版本Selection下數字,設置gcc完成

驗證gcc
安裝vim

$sudo apt-get install vim-gtk

在任意文件夾下建立 .c文件

$sudo mkdir test
$cd test
$sudo vi test.c #新建test.c文件,並進入hello.c編輯


在test.c下手動添加代碼保存

 #include<stdio.h>
int main()
{
printf(“Complete validation!”);
 }
編譯first.c文件

$gcc first.c -o first


生成test可執行文件,則gcc工作正常
參考文獻

https://blog.csdn.net/qinglu000/article/details/22890419?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2
https://blog.csdn.net/earbao/article/details/53495228
https://blog.csdn.net/sslj81/article/details/102014787
https://blog.csdn.net/qq_31347869/article/details/94379302


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM