怎样编译安装gcc

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

【介绍】

官网: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。

【版本】

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

参考:http://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:

下载: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*

输出:

/root/gcc-8.3.0/x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6
/root/gcc-8.3.0/x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.25
/root/gcc-8.3.0/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6
/root/gcc-8.3.0/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.25
/root/gcc-8.3.0/prev-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6
/root/gcc-8.3.0/prev-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.25
/usr/lib64/libstdc++.so.6.0.19
/usr/lib64/libstdc++.so.6
/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
/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.pyo
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.pyc
/usr/share/gdb/auto-load/usr/lib/libstdc++.so.6.0.19-gdb.py
/usr/share/gdb/auto-load/usr/lib/libstdc++.so.6.0.19-gdb.pyo
/usr/share/gdb/auto-load/usr/lib/libstdc++.so.6.0.19-gdb.pyc
/usr/lib/libstdc++.so.6.0.19
/usr/lib/libstdc++.so.6

更新动态库:

cp /root/gcc-8.3.0/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/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/local/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"

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