Linux gcc 离线安装

>>>文章以流水账方式记录,各位可以简单看完后跳过我出错的部分,提高工作效率。

>>>本文Linux系统信息:

[root@localhost  ~]# more /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)
[root@localhost ~]# more /proc/version
Linux version 3.10.0-123.el7.x86_64 (mockbuild@x86-017.build.eng.bos.redhat.com)

>>>刚安装好的Linux系统,很干净, 我们记录下整个过程的详细gcc源码包安装,如果只想看gcc的rpm包安装,请直接跳到第5节。

1、查看系统是否有安装过gcc编译器?

[root@localhost ~]# gcc -v
-bash: gcc: 未找到命令

2、下载gcc

在GCC网站上(http://gcc.gnu.org)或者通过网上搜索可以查找到下载资源。可供下载的文件一般有两种形式:gcc-9.2.0.tar.gz和gcc-9.2.0.tar.bz2,只是压缩格式不一样,内容完全一致,下载其中一种即可。

3、解压缩

上传gcc-9.2.0.tar.gz(我下载的压缩文件)到/usr/src(根据自己喜好选择)下,解包:gz格式:tar zxvf gcc-9.2.0.tar.gz

                                                                                                                                bz2格式:tar jxvf gcc-9.2.0.tar.bz2

新生成的gcc-9.2.0这个目录被称为源目录,我们用{srcdir}来表示。可以使用pwd命令来查看当前路径:

[root@localhost src]# pwd
/usr/src/gcc-9.2.0

4、建立目录

编译目录我们用{objdir}来表示,是用来存放编译结果的地方。GCC建议编译后的文件不要放在源目录{srcdir]中(虽然这样做也能),但最好还是单独存放在另外一个目录中,而且不能是${srcdir}的子目录

例如,可以这样建立一个叫 /usr/local/gcc-9.2.0-build的编译目录:
[root@localhost src]# mkdir /usr/local/gcc-9.2.0-build
[root@localhost src]# cd /usr/local/gcc-9.2.0-build
[root@localhost gcc-9.2.0-build]#

以下的操作主要是在编译目录 {objdir} 下进行。

5、安装

>>>配置的目的是决定将GCC编译器安装到什么地方(我们用{destdir}来表示安装目录),支持什么语言以及指定其它一些选项等。其中,{destdir}不能与{objdir}或{srcdir}目录相同。

{srcdir}:源目录,/usr/src/gcc-9.2.0
{objdir}:编译目录,/usr/local/gcc-9.2.0-build
{destdir}:安装目录,/usr/local/gcc-9.2.0

>>>配置是通过执行${srcdir}下的configure来完成的。其命令格式为:

{srcdir}/configure --prefix=${destdir} [其它选项]

>>>例如,如果想将gcc-9.2.0安装到/usr/local/gcc-9.2.0目录下,则{destdir}就表示这个路径。在我的机器上,我是这样配置的:

[root@localhost gcc-9.2.0-build]# /usr/src/gcc-9.2.0/configure --prefix=/usr/local/gcc-9.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++,java

>>>我这样配置后出现错误提示:configure: error: no acceptable C compiler found in $PATH,表示我们的系统里没有安装任何C语言编译器,之所以报这样的错误是因为我安装gcc是用的源码,下载gcc对应的rpm包安装(低版本即可),提供几个获取RPM资源站点:

http://mirrors.163.com/

http://mirrors.163.com/centos/6/os/x86_64/Packages/

https://opsx.alibaba.com/mirror

http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/

http://rpm.pbone.net/

>>>这是我下载的RPM包地址:http://rpm.pbone.net/index.php3/stat/4/idpl/55095007/dir/redhat_el_7/com/gcc-4.8.2-16.el7.x86_64.rpm.html

>>>安装gcc-4.8.2-16.el7.x86_64.rpm:

[root@localhost gcc-9.2.0-build]# mkdir /opt/rpm
[root@localhost gcc-9.2.0-build]# cd /opt/rpm/
>>>上传gcc-4.8.2-16.el7.x86_64.rpm到/opt/rpm
[root@localhost opt]# rpm -ivh rpm/gcc-4.8.2-16.el7.x86_64.rpm
错误:依赖检测失败:
        cpp = 4.8.2-16.el7 被 gcc-4.8.2-16.el7.x86_64 需要
        glibc-devel >= 2.2.90-12 被 gcc-4.8.2-16.el7.x86_64 需要
        libmpc.so.3()(64bit) 被 gcc-4.8.2-16.el7.x86_64 需要
        libmpfr.so.4()(64bit) 被 gcc-4.8.2-16.el7.x86_64 需要
        glibc-headers 被 glibc-devel-2.17-55.el7.x86_64 需要
        glibc-headers = 2.17-55.el7 被 glibc-devel-2.17-55.el7.x86_64 需要
        kernel-headers 被 glibc-headers-2.17-55.el7.x86_64 需要
        kernel-headers >= 2.2.1 被 glibc-headers-2.17-55.el7.x86_64 需要

>>>下载上面需要的依赖项到/opt/rpm,依次安装:

[root@localhost opt]# rpm -ivh cpp-4.1.2-48.el5.i386.rpm
[root@localhost opt]# rpm -ivh kernel-headers-2.6.18-194.el5.i386.rpm
[root@localhost opt]# rpm -ivh glibc-headers-2.5-49.i386.rpm
[root@localhost opt]# rpm -ivh glibc-devel-2.5-49.i386.rpm
[root@localhost opt]# rpm -ivh libmpc-1.0.2-11.1.x86_64.rpm
[root@localhost opt]# rpm -ivh mpfr-3.1.1-4.el7.x86_64.rpm
[root@localhost opt]# rpm -ivh gcc-4.1.2-48.el5.i386.rpm

>>>或者全部安装
[root@localhost opt]#rpm -ivh rpm/*.rpm --nodeps --force
[root@localhost opt]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)

>>>再来执行GCC配置

[root@localhost opt]# cd /usr/local/gcc-9.2.0-build/
[root@localhost gcc-9.2.0-build]# /usr/src/gcc-9.2.0/configure --prefix=/usr/local/gcc-9.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++,java


>>>提示错误:

configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations.  Source code for these libraries can be found at
their respective hosting sites as well as at
ftp://gcc.gnu.org/pub/gcc/infrastructure/.  See also
http://gcc.gnu.org/install/prerequisites.html for additional info.  If
you obtained GMP, MPFR and/or MPC from a vendor distribution package,
make sure that you have installed both the libraries and the header
files.  They may be located in separate packages.

>>>说明还需要GMP、MPFR、MPC这三个库,从上面的网站下载相应的压缩包。由于MPFR依赖GMP,而MPC依赖GMP和MPFR,所以要先安装GMP,其次MPFR,最后才是MPC。

>>>下载上面需要的依赖项到/usr/src,开始安装GMP

[root@localhost src]# tar jxvf gmp-6.1.0.tar.bz2
tar (child): bzip2:无法 exec: 没有那个文件或目录
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

>>>缺少bzip2包,下载bzip2包并安装,参考:https://blog.csdn.net/qq_21383435/article/details/79539311

>>>安装BZIP2:

[root@localhost src]# tar -zxvf bzip2-1.0.6.tar.gz
[root@localhost src]# cd bzip2-1.0.6
[root@localhost bzip2-1.0.6]# make -f Makefile-libbz2_so
[root@localhost bzip2-1.0.6]# make && make install

>>>安装GMP

[root@localhost bzip2-1.0.6]# cd ..
[root@localhost src]# tar jxvf gmp-6.1.0.tar.bz2
[root@localhost src]# mkdir /usr/local/gmp-6.1.0
[root@localhost src]# cd /usr/local/gmp-6.1.0
[root@localhost gmp-6.1.0]# /usr/src/gmp-6.1.0/configure --prefix=/usr/local/gmp-6.1.0 --enable-cxx

error: No usable m4 in $PATH or /usr/5bin 

>>>提示没有M4,下载M4安装。

[root@localhost rpm]# rpm -ivh m4-1.4.18-69.1.x86_64.rpm
>>>再安装GMP

[root@localhost src]# cd /usr/local/gmp-6.1.0
[root@localhost gmp-6.1.0]# /usr/src/gmp-6.1.0/configure --prefix=/usr/local/gmp-6.1.0 --enable-cxx
[root@localhost gmp-6.1.0]# make
[root@localhost gmp-6.1.0]# make install

>>>安装MPFR

[root@localhost gmp-6.1.0]# cd /usr/src
[root@localhost src]# tar jxvf mpfr3.1.4.tar.bz2
[root@localhost src]# mkdir /usr/local/mpfr-3.1.4
[root@localhost src]# cd /usr/local/mpfr-3.1.4
[root@localhost mpfr-3.1.4]# /usr/src/mpfr-3.1.4/configure --prefix=/usr/local/mpfr-3.1.4 --with-gmp=/usr/local/gmp-6.1.0

(注意配置的时候要把依赖关系选项加进去)

[root@localhost mpfr-3.1.4]# make
[root@localhost mpfr-3.1.4]# make install

>>>安装MPC

[root@localhost gmp-6.1.0]# cd /usr/src
[root@localhost src]# tar zxvf mpc-1.0.3.tar.gz
[root@localhost src]# mkdir /usr/local/mpc-1.0.3
[root@localhost src]# cd /usr/local/mpc-1.0.3
[root@localhost mpfr-3.1.4]# /usr/src/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
[root@localhost mpfr-3.1.4]# make
[root@localhost mpfr-3.1.4]# make install

>>>再次安装GCC,配置安装选项

[root@localhost mpfr-3.1.4]# cd /usr/local/gcc-9.2.0-build
[root@localhost gcc-9.2.0-build]# /usr/src/gcc-9.2.0/configure --prefix=/usr/local/gcc-9.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++,java --with-gmp=/usr/local/gmp-6.1.0 --with-mpfr=/usr/local/mpfr-3.1.4 --with-mpc=/usr/local/mpc-1.0.3

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++

>>>提示出错,拿掉Java配置

[root@localhost gcc-9.2.0-build]# /usr/src/gcc-9.2.0/configure --prefix=/usr/local/gcc-9.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.1.0 --with-mpfr=/usr/local/mpfr-3.1.4 --with-mpc=/usr/local/mpc-1.0.3

configure: error:
The following requested languages could not be built: c++
Supported languages are: c,brig,c,c++,d,fortran,go,lto,objc,obj-c++

>>>还会出错,尝试添加环境变量LD_LIBRARY_PATH,以指出前面三个库的位置,键入以下命令:
[root@localhost gcc-9.2.0-build]# export LD_LIBRARY_PATH=/usr/local/mpc-1.0.3/lib:/usr/local/gmp-6.1.0/lib:/usr/local/mpfr-3.1.4/lib:$LD_LIBRARY_PATH

>>>最好添加到系统的配置文件中,这样就不必要每次都设置这个环境变量了,在文件$HOME/.bash_profile中添加下面两句:

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

export LD_LIBRARY_PATH

>>>重启系统设置生效,或者执行命令:

[root@localhost gcc-9.2.0-build]# source $HOME/.bash_profile

[root@localhost gcc-9.2.0-build]# /usr/src/gcc-9.2.0/configure --prefix=/usr/local/gcc-9.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.1.0 --with-mpfr=/usr/local/mpfr-3.1.4 --with-mpc=/usr/local/mpc-1.0.3

>>>这次没有报错了。

[root@localhost gcc-9.2.0-build]# make

configure: error: C++ preprocessor "/lib/cpp" fails sanity check
>>>这是缺少相应的库导致的报错,安装以下两个包就行:

[root@localhost rpm]# rpm -ivh libstdc++-devel-4.8.2-16.el7.x86_64.rpm
[root@localhost rpm]# rpm -ivh gcc-c++-4.8.2-16.el7.x86_64.rpm


[root@localhost rpm]# cd /usr/local/gcc-9.2.0-build
[root@localhost gcc-9.2.0-build]# make
[root@localhost gcc-9.2.0-build]# make install

make也可以利用4个job进行编译,此值不宜设置过高: make -j4

>>>make大概需要1个多小时的样子,到此安装成功!虽然安装成功,但是并不代表可以使用,需要做环境配置。

6、升级动态链接库

>>>先查看现在动态库的版本

 [root@localhost ~]strings /usr/lib64/libstdc++.so.6 | grep GLIBC

GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_3.4.18
GLIBCXX_3.4.19
GLIBC_2.3
GLIBC_2.2.5
GLIBC_2.14

>>>再查找最新动态库:

[root@localhost local]# find / -name "libstdc++.so"
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/32/libstdc++.so
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/libstdc++.so
/usr/local/gcc-9.2.0-build/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so
/usr/local/gcc-9.2.0-build/x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so
/usr/local/gcc-9.2.0-build/prev-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so
/usr/local/gcc-9.2.0/lib64/libstdc++.so

>>>更新动态库:

[root@localhost ~]# cd /usr/lib64/
[root@localhost lib64]# mv libstdc++.so.6 /tmp/
[root@localhost lib64]# ln -s /usr/local/gcc-9.2.0/lib64/libstdc++.so libstdc++.so.6

>>>更新完成后我们再来看看动态库版本:

[root@localhost lib64]# strings /usr/lib64/libstdc++.so.6 | grep GLIBC

GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_3.4.18
GLIBCXX_3.4.19
GLIBCXX_3.4.20
GLIBCXX_3.4.21
GLIBCXX_3.4.22
GLIBCXX_3.4.23
GLIBCXX_3.4.24
GLIBCXX_3.4.25
GLIBCXX_3.4.26
GLIBCXX_3.4.27
GLIBC_2.2.5
GLIBC_2.3
GLIBC_2.14
GLIBC_2.6
GLIBC_2.4
GLIBC_2.16
GLIBC_2.17
GLIBC_2.3.2

>>>从上面可以看到动态库的版本已经更新

 

7、新老版本gcc共存的解决办法

>>>我们来看看gcc版本

[root@localhost lib64]# gcc -v

使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)

>>>我们看到gcc的版本还是之前使用gcc-4.8.2rpm安装的配置,并不是我们gcc-9.2.0的配置。那因为在bin下面的gcc还是是我们之前安装的,再来看看gcc的链接

[root@localhost lib64]# which gcc
/usr/bin/gcc
[root@localhost lib64]# ls /usr/bin/gcc -i
68837775 /usr/bin/gcc
[root@localhost lib64]# find / -inum 68837775
/usr/bin/gcc
/usr/bin/x86_64-redhat-linux-gcc

>>>可以看出,确实是之前的gcc

>>>解决方式一,我们可以使用链接的方式使新老版本的gcc都可以兼容使用:

[root@localhost bin]# cd /usr/bin
[root@localhost bin]# ln -s {destdir}/bin/gcc gcc920
[root@localhost bin]# ln -s {destdir}/bin/g++ g++920
[root@localhost bin]# ln -s {destdir}/bin/gcj gcj920 

{destdir}:gcc-9.2.0的安装目录

>>>解决方式二,直接覆盖原gcc版本

[root@localhost lib64]# cd /usr/bin/
[root@localhost bin]# mv gcc /tmp/
[root@localhost bin]# ln -s /usr/local/gcc-9.2.0/bin/gcc gcc
[root@localhost bin]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc-9.2.0/libexec/gcc/x86_64-pc-linux-gnu/9.2.0/lto-wrapper
目标:x86_64-pc-linux-gnu
配置为:/usr/src/gcc-9.2.0/configure --prefix=/usr/local/gcc-9.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.1.0 --with-mpfr=/usr/local/mpfr-3.1.4 --with-mpc=/usr/local/mpc-1.0.3
线程模型:posix
gcc 版本 9.2.0 (GCC)

>>>建立好软链接后,以后就会使用新的gcc版本进行编译了。用同样的方法把g++,gcj也都统一覆盖更新一下。至此GCC离线安装圆满完成,写文章+安装+测试总共花了我2天时间。


最后附带我这次安装所需的所有文件,供大家参考使用:

https://download.csdn.net/download/sslj81/11833848


参考资料:

https://www.cnblogs.com/chuncn/archive/2010/10/15/1851853.html

http://blog.chinaunix.net/uid-28662931-id-3503637.html

http://blog.chinaunix.net/uid-11344913-id-298949.html

https://blog.csdn.net/yakoo5/article/details/6916163

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值