怎样编译安装GCC

2016年7月27日 | 分类: 【技术】

快速安装:https://amon.org/gcc

【介绍】

官网:http://gcc.gnu.org/
下载:http://ftp.gnu.org/gnu/gcc/
参考:http://www.linuxfromscratch.org/blfs/view/svn/general/gcc.html

当编译新版 nghttp2 时,在 make 时报错:

error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

解决办法:问题出现于最新版 nghttp2 v1.35.1,更新日志里有“src: Require C++14 language feature”。所以需要升级GCC。

备注:Nginx + h2 没有出现这个问题
参考:https://amon.org/nginx

备注:CentOS 8 已默认 GCC 8.2 。
参考:https://amon.org/centos

【版本】

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)

【安装(编译)】

开发环境:

yum groupinstall "Development Tools" -y

参考:https://amon.org/groupinstall

yum install glibc-static libstdc++-static -y

安装依赖包:

依赖 gmp 、 mpfr 、 libmpc 和 isl 。

yum方式安装 gmp、mpfr、libmpc :

yum install gmp-devel mpfr-devel libmpc-devel -y

编译安装 isl :

参考:https://amon.org/isl

安装 GCC 8.3.0:

下载:http://ftp.gnu.org/gnu/gcc/gcc-8.3.0/

wget http://ftp.gnu.org/gnu/gcc/gcc-8.3.0/gcc-8.3.0.tar.xz && xz -d gcc-8.3.0.tar.xz && tar xvf gcc-8.3.0.tar && cd gcc-8.3.0 && ./contrib/download_prerequisites && ./configure --prefix=/usr/local/gcc8 --enable-languages=c,c++ --disable-multilib
nohup make -j4 &
cd /root/gcc-8.3.0 && make install

make 这个步骤需要注意:
1. -j4 选项是 make 对多核处理器的优化。不要使用 -j ,可能会编译失败。
2. 耗时可能在3个小时,可以使用 nohup 设置后台驻留执行。

【配置】

编辑 /root/.bashrc :

最后加上:

export PATH=/usr/local/gcc8/bin:$PATH

使用新版gcc、g++ 替换老版本:

mv /usr/bin/gcc /usr/bin/gcc.4.8.5 && ln -s /usr/local/gcc8/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++.4.8.5 && ln -s /usr/local/gcc8/bin/g++ /usr/bin/g++
update-alternatives --install /usr/bin/cc cc /usr/local/gcc8/bin/gcc 999 

更新动态库:

查看libstdc++.so.6位置,输入:

sudo find / -name libstdc++.so.6* #ls -l libstdc++.so*

输出:

/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.py
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.pyc
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.pyo
/usr/lib64/libstdc++.so.6。4.8.5
/usr/lib64/libstdc++.so.6
/usr/lib64/libstdc++.so.6.0.19
/usr/local/gcc8/lib64/libstdc++.so.6.0.25-gdb.py
/usr/local/gcc8/lib64/libstdc++.so.6
/usr/local/gcc8/lib64/libstdc++.so.6.0.25

更新动态库:

cp /usr/local/gcc8/lib64/libstdc++.so.6.0.25 /usr/lib64
mv /usr/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so.6。4.8.5 && ln -s libstdc++.so.6.0.25 /usr/lib64/libstdc++.so.6
ldconfig

安装完成。

注意:本步骤必须完成,否则报错。

【验证】

验证版本:

输入:

gcc -v

输出:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc8/libexec/gcc/x86_64-pc-linux-gnu/8.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --prefix=/usr/local/gcc8 --enable-languages=c,c++ --disable-multilib
Thread model: posix
gcc version 8.3.0 (GCC)

编译 nghttp2 :

git clone https://github.com/tatsuhiro-t/nghttp2.git && cd nghttp2
autoreconf -i && automake && autoconf
export PYTHONPATH=/usr/lib64/python2.7/site-packages/ LIBSPDYLAY_CFLAGS="-I/usr/local/include/spdylay" LIBSPDYLAY_LIBS="-L/usr/local/lib -lspdylay" JANSSON_CFLAGS="-I/usr/local/include" JANSSON_LIBS="-L/usr/local/lib -ljansson" OPENSSL_CFLAGS="-I/usr/include/openssl" OPENSSL_LIBS="-L/usr/lib -lssl -lcrypto" CUNIT_CFLAGS="-I/usr/include/CUnit" CUNIT_LIBS="-L/usr/lib -lcunit"  LIBEVENT_OPENSSL_CFLAGS="-I/usr/include" LIBEVENT_OPENSSL_LIBS="-L/usr/lib -levent_openssl -levent" LIBCARES_CFLAGS="-I/usr/local/include" LIBCARES_LIBS="-L/usr/local/lib -lcares"
./configure --with-boost-asio --enable-asio-lib --with-boost-libdir=/usr/lib && make && make install

./configure 时输出:

...
checking whether g++ supports C++14 features with -std=c++14... yes
...
configure: summary of build options:

    Package version: 1.37.0-DEV
    Library version: 31:1:17
    Install prefix:  /usr/local
    System types:
      Build:         x86_64-unknown-linux-gnu
      Host:          x86_64-unknown-linux-gnu
      Target:        x86_64-unknown-linux-gnu
    Compiler:
      C compiler:     gcc
      CFLAGS:         -g -O2
      LDFLAGS:
      C++ compiler:   g++ -std=c++14
      CXXFLAGS:       -g -O2
      CXXCPP:         g++ -E -std=c++14
      C preprocessor: gcc -E
      CPPFLAGS:
      WARNCFLAGS:
      WARNCXXFLAGS:
      CXX1XCXXFLAGS:
      EXTRACFLAG:     -fvisibility=hidden
      LIBS:
...

然后顺利编译成功。

【排错】

ldconfig 时报错:ldconfig: /usr/local/lib/libisl.so.15.3.0-gdb.py is not an ELF file – it has the wrong magic bytes at the start.

GCC 安装完成后 执行 ldconfig 命令时出现此错误。

参考:http://gcc.gnu.org/ml/gcc-help/2014-08/msg00053.html
参考:https://blog.csdn.net/alading2009/article/details/20771117
参考:https://blog.csdn.net/refuil/article/details/51479431

解决方法:直接删除该文件。如果什么时候再需要,再重新编译。

rm /usr/local/lib/libisl.so.15.3.0-gdb.py

再次 ldconfig ,不再报错。

make 时报错:xgcc: error trying to exec ‘cc1’: execvp: No such file or directory

参考:http://blog.chinaunix.net/uid-33797-id-3853210.html

yum install gcc-objc gcc-objc++ libobjc

报错:libstdc++.so.6 is not a symbolic link

因为libxerces-c-3.0.so正常情况下应该是一个符号链接,而不是实体文集件,修改其为符号链接即可。

报错:error: ‘for’ loop initial declarations are only allowed in C99 mode

参考:https://stackoverflow.com/questions/24881/how-do-i-fix-for-loop-initial-declaration-used-outside-c99-mode-gcc-error/27225785

For anyone attempting to compile code from an external source that uses an automated build utility such as Make, to avoid having to track down the explicit gcc compilation calls you can set an environment variable. Enter on command prompt or put in .bashrc (or .bash_profile on Mac):

export CFLAGS="-std=c99"

Note that a similar solution applies if you run into a similar scenario with C++ compilation that requires C++ 11, you can use:

export CXXFLAGS="-std=c++11"

恢复环境变量,参考:https://amon.org/unset

【降级】

在升级之后,发现gnutls编译失败,和gcc版本有关,于是考虑降级。

编辑 /root/.bashrc :

把之前升级时加上的注释掉:

# export PATH=/usr/local/gcc8/bin:$PATH

替换gcc、g++ 版本:

mv /usr/bin/gcc /usr/bin/gcc.8.3.0 && ln -s /usr/bin/gcc.4.8.5 /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++.8.3.0 && ln -s /usr/bin/g++.4.8.5 /usr/bin/g++
update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 1000

【再升级】

在降级之后,想升级不用再次编译。

编辑 /root/.bashrc :

把之前升级时加上的注释去掉:

export PATH=/usr/local/gcc8/bin:$PATH

替换gcc、g++ 版本:

mv /usr/bin/gcc /usr/bin/gcc.485 && ln -s /usr/local/gcc8/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++.485 && ln -s /usr/local/gcc8/bin/g++ /usr/bin/g++
update-alternatives --install /usr/bin/cc cc /usr/local/gcc8/bin/gcc 999 

【安装(yum|CentOS 8)】

CentOS 8,已通过 yum 安装 GCC 8.2 ,当编译安装 Boost 模块时,报错:

no command provided, default command 'g++' not found

参考:https://jingyan.baidu.com/article/915fc414b5f5a351384b206d.html

原因:系统默认安装了gcc,但没有安装g++。

查询是否缺失相对应的包

rpm -qa | grep "g++"

命令解析:rpm为包管理命令,-q表示查询,-a表示所有的包,grep “g++”为过滤条件,如果没安装则不显示其他内容

确认系统是否安装:

在确认系统未安装之后,查询可安装的相对应的功能的包:

yum whatprovides "*/g++"

命令解析:该命令为反查命令, 此命令的含义是查询g++在哪个安装包里面

上面步骤应该会查询到安装包,则输入下面命令安装

yum install gcc-c++-8.2.1-3.5.el8.x86_64

查看版本:

g++ --version

输出:

g++ (GCC) 8.2.1 20180905 (Red Hat 8.2.1-3)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

报错: unnecessary parentheses in declaration of ‘assert_not_arg

参考:https://github.com/boostorg/mpl/issues/31

略过倒数第二步:

./b2 stage threading=multi link=shared

报错: bzlib.h: No such file or directory

参考:https://www.cnblogs.com/qq952693358/p/8563048.html
参考:https://blog.csdn.net/u011808596/article/details/78176465

参考:https://amon.org/bzip

yum install bzip2-devel

报错:auto_ptr’ is deprecated

参考:https://github.com/TGAC/KAT/issues/32

boost 1.58 and 1.59, they both have the same problem

【安装(yum)】

尚未实证。

参考:https://my.oschina.net/michaelshu/blog/3024970

Centos 7.6 yum升级GCC8版本:

安装scl源:

yum install centos-release-scl scl-utils-build
yum load-transaction /tmp/yum_save_tx.2019-03-20.16-33.GsJYkI.yumtx

列出scl可用源:

yum list all --enablerepo='centos-sclo-rh'
yum list all --enablerepo='centos-sclo-rh' | grep "devtoolset-"

安装8版本的gcc、gcc-c++、gdb工具链(toolchian):

yum install -y devtoolset-8-toolchain
scl enable devtoolset-8 bash

验证:

gcc --version