CentOS7默认的GCC版本是4.8,不支持C++14,对C++11支持也不完善,编译时需要加上-std=c++11才能勉强支持C++11,所以对GCC来个升级。
因为3.10的内核实在太老,加之GCC4.8和最新的GCC8.2代差太大,直接用GCC4.8编译安装GCC7或8是不能成功的,我选择了GCC6.5,6.5对C++11支持很完美,还支持C++14.
这里列出了GCC主要的几个镜像站,很遗憾亚洲只有日本一个,国内一个都没有
https://gcc.gnu.org/mirrors.html
Our releases are available on the GNU FTP server and its mirrors. The following sites mirror the gcc.gnu.org FTP site (Phoenix, Arizona, USA) directly:
以我的经验,旧金山和圣何塞的镜像站最快,国内中科大和清华也可以下载,不过md5检验值与上面镜像站不一致,还是乖乖的从上面下载吧,或许这就是为什么上面没有列出科大源或者清华源的原因吧。
下面以旧金山镜像为例
wget https://bigsearcher.com/mirrors/gcc/releases/gcc-6.5.0/gcc-6.5.0.tar.gz
解压和下载依赖项,在解压之后的源码目录contrib下有一个名为download_prerequisites的文件,运行它可以直接下载mpfr之类的东西,这就是考验耐心的时候了,国内下载速度超慢!
$./contrib/download_prerequisites
不过你可以修改一下上面这个脚本,把默认的ftp://gcc.gnu.org替换成其它的镜像链接,比如圣何塞的镜像就很快,速度大概140KB/s,这已经是最快的了
#! /bin/sh
# Download some prerequisites needed by gcc.
# Run this from the top level of the gcc source tree and the gcc
# build will do the right thing.
#
# (C) 2010 Free Software Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
# If you want to disable Graphite loop optimizations while building GCC,
# DO NOT set GRAPHITE_LOOP_OPT as yes so that the isl package will not
# be downloaded.
GRAPHITE_LOOP_OPT=yes
if [ ! -e gcc/BASE-VER ] ; then
echo "You must run this script in the top level GCC source directory."
exit 1
fi
# Necessary to build GCC.
MPFR=mpfr-2.4.2
GMP=gmp-4.3.2
MPC=mpc-0.8.1
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPFR.tar.bz2 || exit 1
tar xjf $MPFR.tar.bz2 || exit 1
ln -sf $MPFR mpfr || exit 1
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$GMP.tar.bz2 || exit 1
tar xjf $GMP.tar.bz2 || exit 1
ln -sf $GMP gmp || exit 1
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPC.tar.gz || exit 1
tar xzf $MPC.tar.gz || exit 1
ln -sf $MPC mpc || exit 1
# Necessary to build GCC with the Graphite loop optimizations.
if [ "$GRAPHITE_LOOP_OPT" = "yes" ] ; then
ISL=isl-0.15
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$ISL.tar.bz2 || exit 1
tar xjf $ISL.tar.bz2 || exit 1
# Fix trailing comma which errors with -pedantic for host GCC <= 4.3
sed -e 's/isl_stat_ok = 0,/isl_stat_ok = 0/' isl-0.15/include/isl/ctx.h > isl-0.15/include/isl/ctx.h.tem && mv isl-0.15/include/isl/ctx.h.tem isl-0.15/include/isl/ctx.h
ln -sf $ISL isl || exit 1
fi
如果是歪果的云主机不用修改,速度也很快
$ ./contrib/download_prerequisites
--2018-12-30 02:43:45-- ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2
=> ‘mpfr-2.4.2.tar.bz2.1’
Resolving gcc.gnu.org (gcc.gnu.org)... 209.132.180.131
Connecting to gcc.gnu.org (gcc.gnu.org)|209.132.180.131|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done. ==> PWD ... done.
==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done.
==> SIZE mpfr-2.4.2.tar.bz2 ... 1077886
==> PASV ... done. ==> RETR mpfr-2.4.2.tar.bz2 ... done.
Length: 1077886 (1.0M) (unauthoritative)
100%[====================================================================================>] 1,077,886 5.08MB/s in 0.2s
2018-12-30 02:43:46 (5.08 MB/s) - ‘mpfr-2.4.2.tar.bz2.1’ saved [1077886]
--2018-12-30 02:43:46-- ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2
=> ‘gmp-4.3.2.tar.bz2’
Resolving gcc.gnu.org (gcc.gnu.org)... 209.132.180.131
Connecting to gcc.gnu.org (gcc.gnu.org)|209.132.180.131|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done. ==> PWD ... done.
==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done.
==> SIZE gmp-4.3.2.tar.bz2 ... 1897483
==> PASV ... done. ==> RETR gmp-4.3.2.tar.bz2 ... done.
Length: 1897483 (1.8M) (unauthoritative)
100%[====================================================================================>] 1,897,483 7.37MB/s in 0.2s
2018-12-30 02:43:46 (7.37 MB/s) - ‘gmp-4.3.2.tar.bz2’ saved [1897483]
--2018-12-30 02:43:47-- ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-0.8.1.tar.gz
=> ‘mpc-0.8.1.tar.gz’
Resolving gcc.gnu.org (gcc.gnu.org)... 209.132.180.131
Connecting to gcc.gnu.org (gcc.gnu.org)|209.132.180.131|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done. ==> PWD ... done.
==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done.
==> SIZE mpc-0.8.1.tar.gz ... 544950
==> PASV ... done. ==> RETR mpc-0.8.1.tar.gz ... done.
Length: 544950 (532K) (unauthoritative)
100%[====================================================================================>] 544,950 --.-K/s in 0.1s
2018-12-30 02:43:48 (3.55 MB/s) - ‘mpc-0.8.1.tar.gz’ saved [544950]
--2018-12-30 02:43:48-- ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.15.tar.bz2
=> ‘isl-0.15.tar.bz2’
Resolving gcc.gnu.org (gcc.gnu.org)... 209.132.180.131
Connecting to gcc.gnu.org (gcc.gnu.org)|209.132.180.131|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done. ==> PWD ... done.
==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done.
==> SIZE isl-0.15.tar.bz2 ... 1574964
==> PASV ... done. ==> RETR isl-0.15.tar.bz2 ... done.
Length: 1574964 (1.5M) (unauthoritative)
100%[====================================================================================>] 1,574,964 6.25MB/s in 0.2s
2018-12-30 02:43:48 (6.25 MB/s) - ‘isl-0.15.tar.bz2’ saved [1574964]
configure参数可以在现有的GCC上修改,这是系统自带的GCC configure信息
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../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
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
把不需要的信息删掉,比如objc,obj-c++,java,fortran,ada,go,lto都不要,只要C和C++,bugurl也不需要,如果需要multilib,还要安装32位的glibc、libstdc++以及zlib
这是我的一个配置
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/6.5.0/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++ --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 6.5.0 (GCC)
在源码目录下新建一个文件夹用于存放编译生成的临时文件
$mkdir build
然后开始
$../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++ --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
上面的configure没错误的话,
$make
make时可以开启多线程,make结束之后,最好先卸载现有的gcc和gcc-c++再安装新版gcc
$make install
$gcc -v
$g++ -v
都能输出version信息基本就没问题
为了保险起见,最好试着编译一个64位和32位的程序
/usr/bin/ld: cannot find crt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: cannot find -lc
/usr/bin/ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
configure: error: 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位lib
checking how to run the C++ preprocessor... /lib/cpp
configure: error: in `/home/nereus/downloads/gcc-src/build/gcc':
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
See `config.log' for more details.
解决:
缺失glibc-headers或g++
CentOS安装
$yum install glibc-headers
$yum install gcc-c++
Debian安装
$apt-get install build-essential
$apt-get install g++
configure: error: changes in the environment can compromise the build
configure: error: run `make distclean' and/or `rm ./config.cache' and start over
make[2]: *** [configure-stage1-gmp] Error 1
make[2]: Leaving directory `/home/nereus/downloads/gcc-src/build'
make[1]: *** [stage1-bubble] Error 2
make[1]: Leaving directory `/home/nereus/downloads/gcc-src/build'
make: *** [all] Error 2
解决方法:上面已经给出了run `make distclean' and/or `rm ./config.cache' and start over