安装包下载

下载源代码包,从gnu网站下载
http://ftp.gnu.org/gnu/gcc/
解压下载包
tar -jxvf gcc-4.9.4.tar.bz2

下载编译所需的依赖包

直接运行:
./contrib/download_prerequisites

出现的问题1
机器不能上网,会出示如下错误:

#./contrib/download_prerequisites
--2019-04-27 23:22:23--  ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2
           => “mpfr-2.4.2.tar.bz2”
Resolving gcc.gnu.org... failed: No address associated with hostname.
wget: unable to resolve host address “gcc.gnu.org”

直接打开文本文件 download_prerequisites,即可看到需要下载的文件。 需要把里面依赖的库下载回来后,放在指定目录,然后注释下载命令,再次执行。
contrib/download_prerequisites,默认会执行相关文件解压,并建立链接。
#vi contrib/download_prerequisites
注释 wget 相关的内容,手动把这些文件下载回来,拷贝到工作目录 gcc-4.9.4下。
执行./contrib/download_prerequisites 出现如下错误。

#./contrib/download_prerequisites
checking for objdir... .libs
checking for the correct version of gmp.h... no
configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations.  Source code for these libraries can be found at
their respective hosting sites as well as at
ftp://gcc.gnu.org/pub/gcc/infrastructure/.  See also
http://gcc.gnu.org/install/prerequisites.html for additional info.  If
you obtained GMP, MPFR and/or MPC from a vendor distribution package,
make sure that you have installed both the libraries and the header
files.  They may be located in separate packages.

需要下载依赖包,有 mpfr-2.4.2.tar.bz2, gmp-4.3.2.tar.bz2, mpc-0.8.1.tar.gz ,isl-0.12.2.tar.bz2,cloog-0.18.1.tar.gz 。
打开 download_prerequisites文件看到如下代码,复制ftb地址下载链接。

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.12.2
  CLOOG=cloog-0.18.1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$ISL.tar.bz2 || exit 1
  tar xjf $ISL.tar.bz2  || exit 1
  ln -sf $ISL isl || exit 1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$CLOOG.tar.gz || exit 1
  tar xzf $CLOOG.tar.gz || exit 1
  ln -sf $CLOOG cloog || exit 1
fi

创建编译目录并编译安装

#cd /home/doc/gcc-4.9.4
#mkdir ./build
#cd ./build
#…/configure -enable-checking=release -enable-languages=c,c++ -disable-multilib
#make -j4
#make install
编译后安装到 /usr/local/bin 目录下 。终端输入gcc 即可展示安装的新版本。

出现的问题2

在make -j4时出现下面错误 error: no usable dependency style found。
需要安装gcc ,g++ 。可以参考redhat linux手动RPM安装gcc,g++

checking how to run the C++ preprocessor... /lib/cpp
configure: error: in `/home/doc/gcc-4.9.4/gcc-build-4.9/gcc':
configure: WARNING: C++ preprocessor "/lib/cpp" fails sanity check
See `config.log' for more details.
checking for unordered_map... no
checking for tr1/unordered_map... no
checking for ext/hash_map... no
checking dependency style of g++... none
configure: error: no usable dependency style found
make[2]: *** [configure-stage1-gcc] Error 1
make[2]: Leaving directory `/home/doc/gcc-4.9.4/gcc-build-4.9'
make[1]: *** [stage1-bubble] Error 2
make[1]: Leaving directory `/home/doc/gcc-4.9.4/gcc-build-4.9'
make: *** [all] Error 2

安装成功后测试

root@Master bin]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/4.9.4/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib
Thread model: posix
gcc version 4.9.4 (GCC) 

为了解决“ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.17’ not found”类似的问题,需要手动拷贝libstdc++.so 文件到 /usr/lib64 并创建libstd++.so.6的软链接。具体如下:

[root@Master lib64]# cp /usr/local/lib64/libstdc++.so.6.0.20 /usr/lib64/
[root@Master lib64]# ln -sf libstdc++.so.6.0.20 libstdc++.so.6
Logo

更多推荐