CentOS 7安装GCC-8.3.0及其依赖


1. 前言

为体验C++17和C++20特性,需安装更新版本的GCC编译器。GCC官网为:https://gcc.gnu.org/,从这里可以下载最新版本的GCC。

C++由Bjarne Stroustrup(被誉为C++之父)于1979年在新泽西州美利山贝尔实验室开始设计开发的,最初命名为带类的C,后来在1983年更名为C++。

2. GCC国内镜像下载地址

下载速度不一,请选择速度最快的:

1) http://mirrors.nju.edu.cn/gnu/gcc/gcc-8.3.0/

2) http://mirrors.ustc.edu.cn/gnu/gcc/gcc-8.3.0/

3) https://mirrors.tuna.tsinghua.edu.cn/gnu/gcc/gcc-8.3.0/

3. GCC的依赖库

编译之前需先安装好GCC的依赖库:gmpmpfrmpc。编译还依赖m4等编译工具,如果没有,则在执行configue时会报相应的错误,这时需要先安装好这些编译工具。

  3.1. gmp库

GMP为“GNU MP Bignum Library”的缩写,是一个GNU开源数学运算库。本文选择的是最新版本gmp-6.1.2,国内镜像下载地址:

1) https://mirrors.tuna.tsinghua.edu.cn/gnu/gmp/

2) http://mirrors.nju.edu.cn/gnu/gmp/

3) http://mirrors.ustc.edu.cn/gnu/gmp/

  3.2. mpfr库

mpfr是一个GNU开源大数运算库,它依赖gmp。本文选择的是最新版本mpfr-4.1.0,国内镜像下载地址:

1) https://mirrors.tuna.tsinghua.edu.cn/gnu/mpfr/

2) http://mirrors.nju.edu.cn/gnu/mpfr/

3) http://mirrors.ustc.edu.cn/gnu/mpfr/

  3.3. mpc库

mpc是GNU的开源复杂数字算法,它依赖gmp和mpfr。本文选择的是最新版本mpc-1.2.1.0,国内镜像下载地址:

1) https://mirrors.tuna.tsinghua.edu.cn/gnu/mpc/

2) http://mirrors.nju.edu.cn/gnu/mpc/

3) http://mirrors.ustc.edu.cn/gnu/mpc/

  3.4. m4编译工具

本文选择的是最新版本m4-1.4.16,下载地址:

1) https://mirrors.tuna.tsinghua.edu.cn/gnu/m4/

2) http://mirrors.nju.edu.cn/gnu/m4/

3) http://mirrors.ustc.edu.cn/gnu/m4/

如果使用“--prefix”指定了安装目录,则在编译gmp等之前还需先设置好环境变量PATH,以便configure时能找到m4。

  3.5. 安装源代码包

涉及到的所有安装源代码包:

gcc-8.3.0.tar.gz
mpfr-4.1.0.tar.gz
gmp-6.1.2.tar.xz
mpc-1.2.1.tar.gz

GCC的依赖库间还互有依赖:mpfr依赖gmp、mpc依赖gmp和mpfr,所以GCC的编译安装顺序为:

1) m4(如果需要,通过yum -y install m4 进行安装)

2) gmp

3) mpfr

4) mpc

5) GCC

为了不污染已有的编译和运行环境,将GCC及依赖库均安装到/usr/local目录,并且最好以root用户完成下述所有操作。

4. 编译安装gmp

执行configure生成Makefile时,需要用到m4,一般路径为/usr/bin/m4,如果没有则需要先安装,否则报错“no usable m4”错误,手工安装m4从“https://www.gnu.org/software/m4/”下载。

具体安装步骤如下:

xz -d gmp-6.1.2.tar.xz
tar xf gmp-6.1.2.tar
cd gmp-6.1.2
./configure --prefix=/usr/local/gmp-6.1.2
make
make install
ln -s /usr/local/gmp-6.1.2 /usr/local/gmp

5. 编译安装mpfr

详细安装步骤如下:

tar xzf  mpfr-4.1.0.tar.gz
cd mpfr-4.1.0
./configure --prefix=/usr/local/mpfr-4.1.0 --with-gmp=/usr/local/gmp
make
make install
ln -s /usr/local/mpfr-4.1.0 /usr/local/mpfr

6. 编译安装mpc

tar xzf  mpc-1.2.1.tar.gz
cd mpc-1.2.1
./configure --prefix=/usr/local/mpc-1.2.1 --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr
make
make install
ln -s /usr/local/mpc-1.2.1 /usr/local/mpc

7. 设置LD_LIBRARY_PATH

在编译GCC之前,如果不设置LD_LIBRARY_PATH(如果编译gmp时没有指定“--prefix”时安装,一般不用再显示设置),则可能编译时报“error while loading shared libraries: libmpfr.so.6: cannot open shared object file: No such file or directory”等错误。

xport LD_LIBRARY_PATH=/usr/local/gmp/lib:/usr/local/mpfr/lib:/usr/local/mpc/lib:$LD_LIBRARY_PATH

8. 编译安装gcc

在执行make编译GCC时,有些费时,请耐心等待。在一台Intel Xeon 2.30GHz的48核128GB内存机器上花费228分钟(将近4个小时,不包括“make install”的时间),编译GCC-8.3.0的GCC版本为4.8.5(64位)。

tar xzf gcc-8.3.0.tar.gz
cd gcc-8.3.0
./configure --prefix=/usr/local/gcc-8.3.0 --with-mpfr=/usr/local/mpfr --with-gmp=/usr/local/gmp --with-mpc=/usr/local/mpc
date;time make;date
make install
ln -s /usr/local/gcc-8.3.0 /usr/local/gcc
export PATH=/usr/local/gcc/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/gcc/lib64:$LD_LIBRARY_PATH
export MANPATH=/usr/local/gcc/share/man:$MANPATH
gcc --version

在执行configure时,如果遇到错误“I suspect your system does not have 32-bit development libraries (libc and headers). If you have them, rerun configure with --enable-multilib. If you do not have them, and want to build a 64-bit-only compiler, rerun configure with --disable-multilib”,表示系统不支持32位程序,这样在执行configure时需为它支持参数“--disable-multilib”,如:

./configure --prefix=/usr/local/gcc-8.3.0 --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr --with-mpc=/usr/local/mpc --disable-multilib

如果make时遇到错误“internal compiler error”,可能是因为内存不足,请换台内存更大的机器,或者更换GCC版本试试。

如果遇到错误“C compiler cannot create executables”、“error while loading shared libraries: libmpfr.so.6: cannot open shared object file: No such file or directory”或“cannot compute suffix of object files”,可尝试设置LD_LIBRARY_PATH后再试试:

export LD_LIBRARY_PATH=/usr/local/gmp/lib:/usr/local/mpfr/lib:/usr/local/mpc/lib:$LD_LIBRARY_PATH

make成功结束时的输出:

make[8]: 离开目录“/data/GCC/gcc-8.3.0/x86_64-pc-linux-gnu/32/libatomic”
make[7]: 离开目录“/data/GCC/gcc-8.3.0/x86_64-pc-linux-gnu/32/libatomic”
make[6]: 离开目录“/data/GCC/gcc-8.3.0/x86_64-pc-linux-gnu/32/libatomic”
make[5]: 离开目录“/data/GCC/gcc-8.3.0/x86_64-pc-linux-gnu/libatomic”
make[4]: 离开目录“/data/GCC/gcc-8.3.0/x86_64-pc-linux-gnu/libatomic”
make[3]: 离开目录“/data/GCC/gcc-8.3.0/x86_64-pc-linux-gnu/libatomic”
make[2]: 离开目录“/data/GCC/gcc-8.3.0/x86_64-pc-linux-gnu/libatomic”
make[1]: 离开目录“/data/GCC/gcc-8.3.0

在完成上列步骤后,检查新安装的GCC-8.3.0是否可用:

# gcc --version
gcc (GCC) 8.3.0
Copyright © 2018 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。
# man gcc|col -b|grep c++17
c++17
GNU dialect of -std=c++17.  The name gnu++1z is deprecated.
This flag is enabled by default for -std=c++17.
arguments as an argument for a template template parameter with fewer template parameters.  This flag is enabled by default for -std=c++17.
adopted for C++17.  Enabled by default with -std=c++17.  -fstrong-eval-order=some enables just the ordering of member access and shift
expressions, and is the default without -std=c++17.
-Wc++17-compat.
"register" keyword as storage class specifier has been deprecated in C++11 and removed in C++17.  Enabled by default with -std=c++17.
-Wc++17-compat (C++ and Objective-C++ only)

已经支持C++20标准,但因为标准还未正式发布,所以正式发布后大概率发生变化:

# man gcc|col -b|grep c++2a
c++2a
GNU dialect of -std=c++2a.  Support is highly experimental, and will almost certainly change in incompatible ways in future releases (支持是高度实验性的,并且在将来的版本中几乎肯定会以不兼容的方式发生变化).

9. 编译安装m4

只有m4不可用或版本过低时才需安装m4(一般路径为/usr/bin/m4),如果configure时不指定prefix,则可省去export和ln两步:

tar xzf m4-1.4.16
cd m4-1.4.16
./configure --prefix=/usr/local/m4-1.4.16
make
make install
ln -s /usr/local/m4-1.4.16 /usr/local/m4
export PATH=/usr/local/m4/bin:$PATH

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM