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

posted @ 2021-12-05 11:13  wangaolin  阅读(2682)  评论(0编辑  收藏  举报