我们知道,关于 GCC 在 CentOS 下通过 yum 安装默认版本号,CentOS 5 是 4.1.2;CentOS 6 是 4.4.7;CentOS 7 是 4.8.3。很多时候在编译安装软件都需要高版本的 GCC,否则就会报错。那么如何升级 GCC 的版本呢?

首先要确认升级到的 GCC 版本号。目前 GCC 的最新版已经到了 9.1(2019-05-03),而 CentOS 7 则依然使用其 4.8,所以基于兼容性考虑,可以选择升级到 4.8.5。GCC 官网:https://gcc.gnu.org

需要注意,编译安装 GCC 内存不小于 1GB,Swap 不小于 1GB,硬盘最低不小于 10GB,否则极有可能会中途报错退出。编译安装完后,目录 gcc-4.8.5 将会有 5GB 之多。

1. 下载源码

  1. wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.8.5/gcc-4.8.5.tar.gz

2. 下载依赖包

编译安装 GCC 需要依赖 mpc,mpfr,gmp,isl 包。前四个包是 GNU 软件,使用 GCC 源码里自带脚本(./contrib/download_prerequisites)可以轻松下载;对于下载不成功的,需要我们手动去下载。

  1. $ tar zxf gcc-4.8.5.tar.gz
  2. $ cd gcc-4.8.5
  3. $ ./contrib/download_prerequisites
  4. --2019-06-06 16:18:09-- ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2
  5. => mpfr-2.4.2.tar.bz2
  6. Resolving gcc.gnu.org... 209.132.180.131
  7. Connecting to gcc.gnu.org|209.132.180.131|:21... connected.
  8. Logging in as anonymous ... Logged in!
  9. ==> SYST ... done. ==> PWD ... done.
  10. ==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done.
  11. ==> SIZE mpfr-2.4.2.tar.bz2 ... 1077886
  12. ==> PASV ... done. ==> RETR mpfr-2.4.2.tar.bz2 ... done.
  13. Length: 1077886 (1.0M) (unauthoritative)
  14. 100%[=================================================================================>] 1,077,886 249K/s in 4.2s
  15. 2019-06-06 16:18:19 (249 KB/s) - mpfr-2.4.2.tar.bz2 saved [1077886]
  16. --2019-06-06 16:18:20-- ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2
  17. => gmp-4.3.2.tar.bz2
  18. Resolving gcc.gnu.org... 209.132.180.131
  19. Connecting to gcc.gnu.org|209.132.180.131|:21... connected.
  20. Logging in as anonymous ... Logged in!
  21. ==> SYST ... done. ==> PWD ... done.
  22. ==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done.
  23. ==> SIZE gmp-4.3.2.tar.bz2 ... 1897483
  24. ==> PASV ... done. ==> RETR gmp-4.3.2.tar.bz2 ... done.
  25. Length: 1897483 (1.8M) (unauthoritative)
  26. 100%[=================================================================================>] 1,897,483 210K/s in 7.7s
  27. 2019-06-06 16:18:30 (239 KB/s) - gmp-4.3.2.tar.bz2 saved [1897483]
  28. --2019-06-06 16:18:31-- ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-0.8.1.tar.gz
  29. => mpc-0.8.1.tar.gz
  30. Resolving gcc.gnu.org... 209.132.180.131
  31. Connecting to gcc.gnu.org|209.132.180.131|:21... connected.
  32. Logging in as anonymous ... Logged in!
  33. ==> SYST ... done. ==> PWD ... done.
  34. ==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done.
  35. ==> SIZE mpc-0.8.1.tar.gz ... 544950
  36. ==> PASV ... done. ==> RETR mpc-0.8.1.tar.gz ... done.
  37. Length: 544950 (532K) (unauthoritative)
  38. 100%[=================================================================================>] 544,950 286K/s in 1.9s
  39. 2019-06-06 16:18:35 (286 KB/s) - mpc-0.8.1.tar.gz saved [544950]
  40. $ wget http://isl.gforge.inria.fr/isl-0.14.tar.gz
  41. $ tar zvxf isl-0.14.tar.gz
  42. $ ln -s lsl isl-0.14

在此脚本里可以看到依赖包的版本号依次是 mpc-0.8.1,mpfr-2.4.2,gmp-4.3.2;另外我们下载 isl-0.14。

在 GCC-7.3.0 中,所有的依赖包都可以通过 ./contrib/download_prerequisites 一键完成下载。
image.png

3. 编译安装

为了避免在编译过程中,受原来系统自定义的一堆环境影响,建议先 unset 这些环境设置:

  1. unset LIBRARY_PATH CPATH C_INCLUDE_PATH PKG_CONFIG_PATH CPLUS_INCLUDE_PATH INCLUDE

参考:https://stackoverflow.com/questions/12255058/g-4-7-1-compilation-error-conflicting-types-for-strsignal

完整编译安装步骤如下:

  1. $ mkdir gcc-build-4.8.5
  2. $ cd gcc-build-4.8.5
  3. $ unset LIBRARY_PATH CPATH C_INCLUDE_PATH PKG_CONFIG_PATH CPLUS_INCLUDE_PATH INCLUDE
  4. $ ../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib --prefix=/Bioinfo/SoftWare/gcc-4.8.5
  5. # 如果想升级 gfortran,切记加上fortran!!!
  6. $ ../configure -enable-checking=release -enable-languages=c,c++,fortran -disable-multilib --prefix=/Bioinfo/SoftWare/gcc-4.8.5
  7. $ make
  8. $ make install
  9. .....
  10. ----------------------------------------------------------------------
  11. Libraries have been installed in:
  12. /Bioinfo/SoftWare/gcc-4.8.5/lib/../lib64
  13. If you ever happen to want to link against installed libraries
  14. in a given directory, LIBDIR, you must either use libtool, and
  15. specify the full pathname of the library, or use the `-LLIBDIR'
  16. flag during linking and do at least one of the following:
  17. - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
  18. during execution
  19. - add LIBDIR to the `LD_RUN_PATH' environment variable
  20. during linking
  21. - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
  22. - have your system administrator add LIBDIR to `/etc/ld.so.conf'
  23. See any operating system documentation about shared libraries for
  24. more information, such as the ld(1) and ld.so(8) manual pages.
  25. ----------------------------------------------------------------------
  26. ......

注意:

  • 如果想升级 gfortran,configure 时切记加上 fortran!!!

如果需要重新 configure 或在 make 中途出错需要退出,最好把当前目录下的所有东西都删除干净。可以执行下面指令清空 编译目录下的相关文件,包括 makefile:

  1. make distclean

4. 配置环境变量

GCC 安装完成后,需要包新安装的 gcc 添加到 PATH 个人相应的 LD_LIBRARY_PATH

  1. export PATH=/Bioinfo/SoftWare/gcc-4.8.5/bin:$PATH
  2. export LD_LIBRARY_PATH=/Bioinfo/SoftWare/gcc-4.8.5/lib64:$LD_LIBRARY_PATH

如果不想把新装 GCC 添加到 ~/.bashrc 中的 PATH 和 LD_LIBRARY_PATH 让它永久起作用,但是在安装软件时又想使用新安装的高级版本的 GCC 来编译软件,可以:

  • 在软件编译前先执行上面两个 export 语句,再执行后面的编译安装命令;
  • 或者在软件编译安装时直接在编译参数中添加 gcc/g++ 和其动态库的路径就可以。

到这里新版本的 GCC 就已经安装完了,在下一篇文章中我们将会跟大家分享一下怎么使用新版本的 GCC 在 Linux 下源码编译安装最新版本的 R-3.6.0。

5. 参考资料

  1. 秋水逸冰,《在CentOS下编译安装GCC》,WorldPress 博客
  2. 佚名,《How can I understand these dreadful errors when building gcc-4.8.2?》,StackOverflow
  3. ljpwinxp,《Centos 6.9 编译安装gcc 4.8.5》,51CTO
  4. 代码日志,《c – 构建gcc-4.8.2时如何理解这些可怕的错误?》, 博客
  5. Benjamin Berhault, 《Build and Install the Last GCC on RHEL/CentOS 7》,Ben’s Jekyll Blog