携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第3天,点击查看活动详情
最近的一个项目中需要用到gcc编译一些源码,但是由于服务器系统是Centos,和Ubuntu还是有些区别的,Centos管理库包的工具是yum,我使用yum安装gcc:yum install gcc-c++ ,然而安装的版本是4.8.5,当你更新的时候,更新完依旧是4.8.5,奔溃!但是,最新版本的gcc已经到10.1.0版本啦!
无奈,只能从源码开始安装
1 下载gcc的源码
1.1 源码下载地址:
1、下载gcc:我下载版本gcc-7.5.0(根据自己需要下载指定版本)
wget https://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.gz
注意:如果下载比较慢,你就直接在浏览器中下载
2、解压下载的源码压缩包
tar -zxf gcc-7.5.0.tar.gz
3、进入源码目录
cd gcc-7.5.0.tar.gz
1.2 下载gcc的依赖库包
主要依赖库包如下:
gmp ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2
mpfr ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
mpc ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz
isl ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.16.1.tar.bz2
下载gcc依赖 当然不一定是这几个版本,无需去查找,只要在进入源码目录中执行如下命令,就会自动下载这几个依赖:
./contrib/download_prerequisites
下载的四个依赖分别为:
- gmp-6.1.0.tar.bz2
- mpfr-3.1.4.tar.bz2
- mpc-1.0.3.tar.gz
- isl-0.16.1.tar.bz2
2、解压编译依赖
解压依赖文件命令:
- tar jxvf gmp-6.1.0.tar.bz2
- tar jxvf mpfr-3.1.4.tar.bz2
- tar zxvf mpc-1.0.3.tar.gz
- tar jxvf isl-0.16.1.tar.bz2
2.1 解压编译依赖gmp-6.1.0.tar.bz2
tar jxvf gmp-6.1.0.tar.bz2
cd gmp-6.1.0
./configure --prefix=/usr/local/gmp
make && make install
注意:
如果在执行./configure --prefix=/usr/local/gmp报错:error: No usable m4 in $PATH or /usr/5bin (see config.log for reasons).,说明没有安装m4,使用yum install m4,然后再执行即可!
2.2 解压编译依赖mpfr-3.1.4.tar.bz2
tar jxvf mpfr-3.1.4.tar.bz2
cd mpfr-3.1.4
./configure --prefix=/usr/local/mpfr
make && make install
如果在执行./configure --prefix=/usr/local/mpfr报错:configure: error: gmp.h can't be found, or is unusable.,是因为缺少lipgmp库,使用:yum install gmp-devel.x86_64安装即可
2.3 解压编译依赖mpc-1.0.3.tar.gz
tar zxvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure --prefix=/usr/local/mpc
make && make install
如果在执行./configure --prefix=/usr/local/mpc报错:error: libmpfr not found or uses a different ABI (including static vs shared)..,是因为缺少lipgmp库,使用: yum install mpfr-devel.x86_64安装即可
2.4 解压编译依赖isl-0.16.1.tar.bz2
tar jxvf isl-0.16.1.tar.bz2
cd isl-0.16.1
./configure --prefix=/usr/local/isl
make && make install
2.5 添加依赖的环境变量
添加环境变量
vi ~/.bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc/lib:/usr/local/gmp/lib:/usr/local/mpfr/lib/:/usr/local/isl/lib
source ~/.bashrc
需要重启系统,让环境变量生效!!!
3 编译gcc
cd gcc-7.5.0
./configure --prefix=/usr/local/gcc --with-gmp=/usr/local/gmp/ --with-mpfr=/usr/local/mpfr/ --with-mpc=/usr/local/mpc/ --with-isl=/usr/local/isl --with-system-zlib --disable-multilib
make && make install
如果执行make && make install编译报错,如下:
.....
ocal/isl/include -o lto-compress.o -MT lto-compress.o -MMD -MP -MF ./.deps/lto-compress.TPo ../.././gcc/lto-compress.c
../.././gcc/lto-compress.c:34:18: 致命错误:zlib.h:没有那个文件或目录
#include <zlib.h>
^
编译中断。
make[3]: *** [lto-compress.o] 错误 1
make[3]: 离开目录“/HDD/tools/gcc-7.5.0/host-x86_64-pc-linux-gnu/gcc”
make[2]: *** [all-stage1-gcc] 错误 2
make[2]: 离开目录“/HDD/tools/gcc-7.5.0”
make[1]: *** [stage1-bubble] 错误 2
make[1]: 离开目录“/HDD/tools/gcc-7.5.0”
make: *** [all] 错误 2
(base) [root@localhost gcc-7.5.0]#
安装zlib:yum install zlib-devel.x86_64
编译的过程比较久,大概编译了三个多小时,建议把:make && make install分开执行,通过多进程执行,如下:
make -j8
make install
编译完之后的结果:
.....
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/gcc/lib/../lib64
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
make[4]: 对“install-data-am”无需做任何事。
make[4]: 离开目录“/HDD/tools/gcc-7.5.0/x86_64-pc-linux-gnu/libatomic”
make[3]: 离开目录“/HDD/tools/gcc-7.5.0/x86_64-pc-linux-gnu/libatomic”
make[2]: 离开目录“/HDD/tools/gcc-7.5.0/x86_64-pc-linux-gnu/libatomic”
make[1]: 离开目录“/HDD/tools/gcc-7.5.0”
(base) [root@localhost gcc-7.5.0]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/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.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/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.5 20150623 (Red Hat 4.8.5-39) (GCC)
(base) [root@localhost gcc-7.5.0]#
但是我们在:/usr/local/gcc/bin下可以看到已经生成可执行文件gcc,并且能够查到版本是:7.5.0
(base) [root@localhost bin]# which gcc
/usr/bin/gcc
(base) [root@localhost bin]# cd /usr/bin
(base) [root@localhost bin]# ./gcc -v
使用内建 specs。
COLLECT_GCC=./gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/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.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/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.5 20150623 (Red Hat 4.8.5-39) (GCC)
(base) [root@localhost bin]#
系统自带的gcc版本的可执行文件为:/usr/bin/gcc,我把这个可执行文件gcc备份成gcc_4.8.5
mv gcc gcc_4.8.5
然后把我自己安装的:gcc-7.5.0 可执行文件链接到该目录下:
ln -s /usr/local/gcc/bin/gcc /usr/bin/gcc
然后测试成功:
(base) [root@localhost ~]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc/libexec/gcc/x86_64-pc-linux-gnu/7.5.0/lto-wrapper
目标:x86_64-pc-linux-gnu
配置为:./configure --prefix=/usr/local/gcc --with-gmp=/usr/local/gmp/ --with-mpfr=/usr/local/mpfr/ --with-mpc=/usr/local/mpc/ --with-isl=/usr/local/isl --with-system-zlib --disable-multilib
线程模型:posix
gcc 版本 7.5.0 (GCC)
(base) [root@localhost ~]#
同样,如果你需要使g++,一样建立软连接配置
参考1:my.oschina.net/u/4393384/b… 参考2:blog.csdn.net/zr107631129… 参考3:bbs.huaweicloud.com/forum/threa… 参考4:www.cnblogs.com/devilmaycry… # 查看安装几个gcc 参考5:my.oschina.net/u/2306127/b… # 版本切换,我测试并并不可以,我的文件好像有些没有