http://www.linuxidc.com/Linux/2015-01/112595.htm


一、简易安装

操作环境 CentOS6.5 64bit,原版本4.4.7,不能支持C++11的特性~,希望升级到4.8.2

不能通过yum的方法升级,需要自己手动下载安装包并编译

1.1 获取安装包并解压
wget http://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.bz2
tar -jxvf gcc-4.8.2.tar.bz2

当然,http://ftp.gnu.org/gnu/gcc  里面有所有的gcc版本供下载,最新版本已经有4.9.2啦.

1.2 下载供编译需求的依赖项

参考文献[1]中说:这个神奇的脚本文件会帮我们下载、配置、安装依赖库,可以节约我们大量的时间和精力。

cd gcc-4.8.0 
./contrib/download_prerequisites 
1.3 建立一个目录供编译出的文件存放
mkdir gcc-build-4.8.2
cd gcc-build-4.8.2
1.4 生成Makefile文件
../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib
1.5 编译(注意:此步骤非常耗时)
make -j4

-j4选项是make对多核处理器的优化,如果不成功请使用 make,相关优化选项可以移步至参考文献[2]。

不建议使用make -j4 方式编译,很可能会失败。-j4选项是make对多核处理器的优化。虽然直接make速度慢点,只要不出错,总体上还是可以接受的。

我在安装此步骤时候出错,错误描述:

 
compilation terminated.
make[5]: *** [_gcov_merge_add.o] 错误 1
make[5]: Leaving directory `/home/imdb/gcc-4.8.2/gcc-build-4.8.2/x86_64-unknown-linux-gnu/32/libgcc'
make[4]: *** [multi-do] 错误 1
make[4]: Leaving directory `/home/imdb/gcc-4.8.2/gcc-build-4.8.2/x86_64-unknown-linux-gnu/libgcc'
make[3]: *** [all-multi] 错误 2
make[3]: *** 正在等待未完成的任务....
make[3]: Leaving directory `/home/imdb/gcc-4.8.2/gcc-build-4.8.2/x86_64-unknown-linux-gnu/libgcc'
make[2]: *** [all-stage1-target-libgcc] 错误 2
make[2]: Leaving directory `/home/imdb/gcc-4.8.2/gcc-build-4.8.2'
make[1]: *** [stage1-bubble] 错误 2
make[1]: Leaving directory `/home/imdb/gcc-4.8.2/gcc-build-4.8.2'
make: *** [all] 错误 2
 

大概看看,错误集中在 x86_64unknown-linux-gnu/32/libgcc 和 x86_64-unknown-linux-gnu/libgcc

根据参考文献[3],安装如下两个软件包(仅用于CentOS6.X):

sudo yum -y install glibc-devel.i686 glibc-devel

过程中CPU基本满载:

1.6、安装
sudo make install

 

二、验证安装

重启,然后查看gcc版本:

gcc -v

尝试写一个C++11特性的程序段 tryCpp11.cc,使用了shared_ptr

 
 1 //tryCpp11.cc
 2 #include <iostream>
 3 #include <memory>
 4 
 5 int main()
 6 {
 7     std::shared_ptr<int> pInt(new int(5));
 8     std::cout << *pInt << std::endl;
 9     return 0;
10 }
 

验证文件:

g++ -std=c++11 -o tryCpp11 tryCpp11.cc
./tryCpp11

Linux升级GCC 4.8.1清晰简明教程(Ubuntu 12.04 64位版为例) http://www.linuxidc.com/Linux/2014-04/99583.htm

在CentOS 6.4中编译安装GCC 4.8.1 + GDB 7.6.1 + Eclipse 在CentOS 6.4中编译安装GCC 4.8.1 + GDB 7.6.1 + Eclipse

Ubuntu下Vim+GCC+GDB安装及使用 http://www.linuxidc.com/Linux/2013-01/78159.htm

Ubuntu下两个GCC版本切换 http://www.linuxidc.com/Linux/2012-10/72284.htm




//--------------------------------------第二种安装方法-------------------------------------------------------------------------------

http://www.cnblogs.com/xlmeng1988/p/5580762.html

安装gcc4.8.5

安装gcc4.8.5
1. g++ --version, 4.4.7不支持c++11
2. 升级gcc-c++, 下载gcc
    https://gcc.gnu.org/ 官网,镜像下载地址https://gcc.gnu.org/mirrors.html,随便找个镜像下载就行了
    http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-4.8.5/gcc-4.8.5.tar.bz2    
3. 解压,配置configure --prefix=yourprefix --enable-languages=c,c++
报错:
configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ 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.

去ftp://gcc.gnu.org/pub/gcc/infrastructure/下载所需的gmp-4.3.2.tar.bz2,mpc-0.8.1.tar.gz,mpfr-2.4.2.tar.bz2
分别解压安装
4. 重新配置,configure --prefix=yourprefix --enable-languages=c,c++ --with-gmp=gmp_path --with-mpc=mpc_path --with-mpfr=mpfr_path
再报错:
checking for suffix of object files... configure: error: in `xxx/gcc-4.8.5/x86_64-unknown-linux-gnu/libgcc':
configure: error: cannot compute suffix of object files: cannot compile
See `config.log' for more details.
make[2]: *** [configure-stage1-target-libgcc] Error 1
make[2]: Leaving directory `xxx/gcc-4.8.5'
不要打开xxx/gcc-4.8.5/config.log, 错误信息xxx/gcc-4.8.5/x86_64-unknown-linux-gnu/libgcc/config.log
共有3类错误信息:
    1. xgcc: error: unrecognized command line option '-V', fatal error: no input files
    2. xgcc: error: unrecognized command line option '-qversion', fatal error: no input files
    3. xxx/gcc-4.8.5/x86_64-unknown-linux-gnu/gcc/cc1: error while loading shared libraries: libmpc.so.2: cannot open shared object file: No such file or directory
Google了半天,结果是1/2类错误可以忽略不计,3类错误我们上面明明已经安装了mpc,也指定了路径,结果还是找不到。。。
需要设置LD_LIBRARY_PATH,export LD_LIBRARY_PATH=mpc_path/lib
参考:http://blog.csdn.net/gaara_fan/article/details/7436940
5. 编译,make -j8
报错: Error “gnu/stubs-32.h: No such file or directory”
原因是缺少32位libc开发包(http://stackoverflow.com/questions/7412548/error-gnu-stubs-32-h-no-such-file-or-directory-while-compiling-nachos-source)
yum install glibc-devel.i686
6. 再编译,make -j8
等待…………………………………………………………………………
7. make install

8. 编写helloworld.cpp,编译: g++ helloworld.cpp
报错: cc1plus: error while loading shared libraries: libmpc.so.2: cannot open shared object file: No such file or directory
add  LD_LIBRARY_PATH=mpc_path/lib
9. 运行,./a.out, OK
10. debug,gdb a.out
加断点,run,Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6_2.9.x86_64 libgcc-4.4.6-3.el6.x86_64 libstdc++-4.4.6-3.el6.x86_64
参考:http://stackoverflow.com/questions/10389988/missing-separate-debuginfos-use-debuginfo-install-glibc-2-12-1-47-el6-2-9-i686
#debuginfo-install glibc-2.12-1.47.el6_2.9.x86_64 libgcc-4.4.6-3.el6.x86_64 libstdc++-4.4.6-3.el6.x86_64
n运行下一条,Single stepping until exit from function main, which has no line number info
参考:http://stackoverflow.com/questions/20474668/gdb-compile-with-g-option-but-single-stepping-until-exit-from-function-main
gdb版本太低……7.2-90.e16
http://ftp.gnu.org/gnu/gdb/下载新版本:gdb-7.6.tar.bz2
解压安装
再次调试,OK……




Logo

更多推荐