CentOS 系统下编译安装 gcc4.8.2过程及编译问题详解

CentOS上源码安装GCC 4.8.2

gcc --version # 查看gcc版本

sudo yum update gcc -y  #只能升到4.4.7

1) 前提

参考Prerequisitesfor GCC,需要GMP, MPFR, MPC, ISL, CLooG。

我先查看了下系统,发现原生就装了GMP等,如下命令。但之后"gcc ./configure"有错,仍需要下载安装GMP等。详见"Issue 1"。

yum list installedgmp # 查看gmp安装了没

ps: 上述问题,应该是要"yum install gmp-devel"来安装开发环境就可以了。原本只有运行环境,所以是"Issue 1"里提到的找不到头文件。MPFR,MPC等也同样。

2) 准备

缺少的都可以先到ftp://gcc.gnu.org/pub/gcc/infrastructure/下找找看。

# 创建个目录,都下载到这里

mkdir -p ~/Env/gcc; cd ~/Env/gcc

 

# GMP, MPFR, MPC

wget https://ftp.gnu.org/gnu/gmp/gmp-5.1.3.tar.bz2

wget http://www.mpfr.org/mpfr-current/mpfr-3.1.2.tar.gz

wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.2.tar.gz

tar jxvf gmp-5.1.3.tar.bz2

tar zxvf mpfr-3.1.2.tar.gz

tar zxvf mpc-1.0.2.tar.gz

# 都直接安装到默认路径下

cd gmp-5.1.3

./configure --enable-cxx CPPFLAGS=-fexceptions # 选项原因"Issue2"

make; make install

cd ../mpfr-3.1.2; ./configure

make; make install

cd ../mpc-1.0.2; ./configure

make; make install

cd ..

如果GMP、MPFR、MPC在"./configure"时用"--prefix"指定了安装路径,则"gcc./configure"需要加上"--with-xxx"等选项。由GMP > MPFR > MPC依赖顺序,也需要加上"--with-xxx"选项。可见参考34。

之后"gcc./configure"(见"3) 安装"),我这儿仍有错,ISL与CLooG也要安装:

sudo gedit config.log  # 查看日志,搜索"error"

 

# issue:configure:error: C++ compilermissing or inoperational

# 没有C++编译器

yum install gcc-c++

 

# issue: conftest.cpp:11:2:error: #error -static-libstdc++ notimplemented

# 没有CC++静态库

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

# 但报"No packagelibstdc++-static available",即没有-static-libstdc++源,问题仍存在。

# "checking whether g++ accepts-static-libstdc++ -static-libgcc"时报出的,可忽略。

 

# issue: conftest.c:10:25:error: isl/version.h: No such file ordirectory

# 没有ISL

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.12.2.tar.bz2

tar jxvf isl-0.12.2.tar.bz2

cd isl-0.12.2; ./configure

make; make install

cd ..

 

# issue: ./conftest: errorwhile loading sharedlibraries: libisl.so.10:cannot open shared objectfile: No such file or directory

# "/usr/local/lib"目录下,怎么就它找不到。加到"/etc/ld.so.conf"或用"LD_LIBRARY_PATH"

vi /etc/ld.so.conf  #添加"/usr/local/lib"

ldconfig # 重建/etc/ld.so.cache

# 自问:于**Issue1**里,已经单独在"/etc/ld.so.conf.d"下建个"*.conf"添加过"/usr/local/lib",怎么没效果呢?

# 自答:之后安装的ISL,没重新ldconfig,所以找不到了?当时没查,不能确认。用以下命令:

strings /etc/ld.so.cache |grep libisl  #查看

# 之后,删除了"/etc/ld.so.conf"内的添加,也不再有该问题。

 

# issue: conftest.c:10:27:error: cloog/version.h: No such file ordirectory

# 没有CLooG

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/cloog-0.18.1.tar.gz

tar zxvf cloog-0.18.1.tar.gz

cd cloog-0.18.1; ./configure

make; make install

cd ..

 

# issue: conftest.c:15:error:'choke' undeclared(first useinthis function)

# "checking for version 0.17.0 ofCLooG"失败报出的,没问题。

如果安装时都自定义了路径,记得要将库路径都添加到"/etc/ld.so.conf"内。

3) 安装

# GCC

wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.gz # 下载gcc源码

tar zxvf gcc-4.8.2.tar.gz # 解压源码

 

cd gcc-4.8.2 # 进入源码目录

# 生成Makefile./configure--help看帮助,或看参考2

./configure \

--prefix=/usr/local/gcc-4.8.2\

--enable-languages=c,c++,go\

--disable-multilib

 

make; make install  #编译并安装

 

# 重建gcc软链接

cd /usr/bin

mv gcc gcc4.4.7

mv g++ g++4.4.7

ln -s /usr/local/gcc-4.8.2/bin/gcc gcc

ln -s /usr/local/gcc-4.8.2/bin/g++ g++

 

# 添加gccman路径

vi /etc/man.config

# 添加"MANPATH/usr/local/gcc-4.8.2/share/man"

# 由于未卸载旧的,我这"mangcc"可以查看帮助,所以未添加。

# 如果再添加,不确定会有什么影响。

4) 问题(每次处理完成问题之后一定要在make clean之后重新执行从3)安装过程的./configure开始,不能直接make)

Issue 1:

king for the correct version of gmp.h... no configure: error: Building GCCrequires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+. Try the --with-gmp, --with-mpfrand/or --with-mpc options to specify their locations.

开始没注意"king for thecorrect version of gmp.h... no",先如下查了gmp等的位置。

rpm -qa|grepgmp # 查看gmp安装了哪些

rpm -ql gmp-4.3.1-7.el6_2.2.i686 # 查看gmp安装位置

然后添加"--with-gmp"等,没效果。

难道库搜索路径没包括"/usr/lib",却包括了"/usr/local/lib"(之后安装了GMP等后,却能通过"./configure")?

于是,查看"/etc/ld.so.conf",其"includeld.so.conf.d/*.conf",但继续查看发现都不包含上述两路径,不是很明白。然后,自行添加了下。

cd /etc/ld.so.conf.d

touch mine.conf  #或用vi,发现我不会用了

gedit mine.conf  #添加/usr/lib/usr/local/lib

ldconfig # 重建/etc/ld.so.cache

注意,安装ISL后,执行ldconfig会有如下错误,重装换个低版本也一样。试着解决没成功,然后忽视了。类似提问:Pacman/ldconfig - wrong magic bytes

ldconfig: /usr/local/lib/libisl.so.10.2.2-gdb.py is not an ELF file - ithas the wrong magic bytes at the start.

vi命令参考[1][2]

之后,发现GMP等头文件没有:

find / |grep gmp.h

所以还是要安装的。

Issue 2:

# 没有PPL

wget ftp://ftp.cs.unipr.it/pub/ppl/releases/1.1/ppl-1.1.tar.bz2

tar jxvf ppl-1.1.tar.bz2

cd ppl-1.1; ./configure

make; make install

cd ..

"ppl ./configure"时,

如果"gmp./configure"未设置"--enable-cxx":

checking for the GMP library version 4.1.3 or above... no configure:error: Cannot find GMP version 4.1.3 or higher. GMP is the GNU Multi-Precisionlibrary: see http://www.swox.com/gmp/ formore information. When compiling the GMP library, do not forget to enable theC++ interface: add --enable-cxx to the configuration options.

如果"gmp./configure"未设置"CPPFLAGS=-fexceptions":

configure: WARNING: CANNOT PROPAGATE EXCEPTIONS BACK FROM GMP: *** MEMORYEXHAUSTION MAY RESULT IN ABRUPT TERMINATION. *** This is OK, if you do not planto use the bounded memory capabilities *** offered by the PPL. Otherwise, ifyou are using GCC or the Intel C/C++ *** compiler, please make sure you use aversion of GMP compiled with the *** `-fexceptions' compiler option. *** Tobuild such a version, you can configure GMP as follows: *** CPPFLAGS=-fexceptions./configure --enable-cxx --prefix=/usr/local

按要求重编了GMP,但以上这个警号还会在。

"ppl make"时,

mp_std_bits.defs.hh:47: error: redefinition of 'classstd::numeric_limits<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>>' /usr/local/include/gmpxx.h:3271: error: previous definition of 'classstd::numeric_limits<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>>' mp_std_bits.defs.hh:108: error: redefinition of 'classstd::numeric_limits<__gmp_expr<__mpq_struct [1], __mpq_struct [1]>>' /usr/local/include/gmpxx.h:3308: error: previous definition of 'classstd::numeric_limits<__gmp_expr<__mpq_struct [1], __mpq_struct [1]>>'

ppl-0.11与gmp-5.1.3有冲突,改为ppl-1.1即可。

ppl-0.11: ftp://gcc.gnu.org/pub/gcc/infrastructure/ppl-0.11.tar.gz

issue3 : fatal error: gnu/stubs-32.h: No such file or directory

64位系统中GCC/UPCmultilib feature可用,但是却没有安装32位的glibc,就会报错(尼玛,为毛要32位的库。。。?)所以解决办法有两个,一个是关闭multilib,一个是安装32位的glibc
 
我选择的是后者。这时候看上面那个图的解决办法,我是Centos 6.5,所以直接把glibc-devel.i686libstdc++-devel.i686全部安装
 sudo yum install glibc-devel.i686libstdc++-devel.i686

 然后重新
 ./configure

  make   && make install

issue 4  configure:error: cannot compute suffix of object files: cannot compile

解决办法: 
我的gmp, mpfr, mpc都是使用默认参数安装的,没指定任何参数

./configure
make
make install

所以直接使用下面的命令设置环境变量就可以了:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

如果安装时指定了安装目录,使用类似下面的命令:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/gcc-4.6.3/mpc-0.9/mpc_install/lib:/opt/gcc-4.6.3/gmp-5.0.4/gmp_install/lib:/opt/gcc-4.6.3/mpfr-3.1.0/mpfr_install/lib

 

5) 测试

test.cc:

#include<iostream>

#include<string>

#include<vector>

template<typename T>

std::ostream&operator<<(std::ostream&os,conststd::vector<T>&vec)

{

    for (auto& el : vec)

    {

       os << el << ' ';

    }

    return os;

}

intmain()

{

    std::vector<std::string> words = {

       "Hello","from", "GCC", __VERSION__,"!"

   };

    std::cout << words<<std::endl;

}

然后:

g++ -std=c++11test.cc -otest

./test

输出:"Hello from GCC4.8.2 !"。

动态库链接问题解决/usr/lib/libstdc++.so.6: version`GLIBCXX_3.4.14' not found问题

我遇到这个问题的时候是在连接库的时候出现的问题,而且不是在编译的时候出现的,实在运行的时候才报错,出现这种问题就是因为编译库的编译器和编译当前程序的编译器版本是不一样的,在具体一点就是因为,当前程序的编译器的版本是比较低的,只要升级一下就可以了。可以用如下命令查看一下当前GCC版本:

[cpp] view plain copy

 

1.  strings /usr/lib/libstdc++.so.6 | grep GLIBCXX  

在我的机器上运行结果这样的:

[python] view plain copy

 

1.  GLIBCXX_3.4  

2. GLIBCXX_3.4.1  

3.  GLIBCXX_3.4.2  

4. GLIBCXX_3.4.3  

5.  GLIBCXX_3.4.4  

6. GLIBCXX_3.4.5  

7.  GLIBCXX_3.4.6  

8. GLIBCXX_3.4.7  

9.  GLIBCXX_3.4.8  

10.GLIBCXX_3.4.9  

11. GLIBCXX_3.4.10  

12.GLIBCXX_3.4.11  

13. GLIBCXX_3.4.12  

14.GLIBCXX_3.4.13  

15. GLIBCXX_FORCE_NEW  

16.GLIBCXX_DEBUG_MESSAGE_LENGTH  


并没有动态库中要求的GCC版本“GLIBCXX_3.4.14”,所以需要进行升级一下我们的GCC版本,升级过程如下:

下载新版本的GCC,地址为点击打开链接

如果是64位系统,执行:wgethttp://ftp.de.debian.org/debian/pool/main/g/gcc-4.7/libstdc++6_4.7.2-5_amd64.deb
下载下来之后,用如下命令开始解压

[html] view plain copy

 <img border="0" width="12" height="12" src="file:///C:/Users/yeshuai/AppData/Local/Temp/msohtmlclip1/01/clip_image001.gif" <="" span="">alt=在CODE上查看代码片 v:shapes="_x0000_i1029"><img border="0" width="12" height="12" src="file:///C:/Users/yeshuai/AppData/Local/Temp/msohtmlclip1/01/clip_image001.gif"' alt=派生到我的代码片 v:shapes="_x0000_i1030">

1.  ar -x libstdc++6_4.7.2-5_i386.deb && tar xvf data.tar.gz  

如果你的文件目录为GCC(如果不是,把GCC改为相应的目录即可),依次执行下面命令:

1、进入到 usr/lib/i380-linux-gun目录下

1.  cd GCC/usr/lib/i380-linux-gun   

2、变成root用户(如果是root用户可跳过)

1.  su root  

3、拷贝文件

1.  cp libstdc++.so.6.0.17 /usr/lib   

4、进入到/usr/lib

1.  cd /usr/lib   

5、删除原来的libstdc++.so.6

1.  rm libstdc++.so.6  

6、重新建立软连接

1.  ln libstdc++.so.6.0.17 libstdc++.so.6  


到现在升级就完成了。我们在去执行以下下面的命令<img border="0" width="12" height="12" src="file:///C:/Users/yeshuai/AppData/Local/Temp/msohtmlclip1/01/clip_image001.gif" <="" span="">style='orphans: auto;text-align:start;white-space:pre-wrap;widows: 1;-webkit-text-stroke-width: 0px;word-spacing:0px' alt=在CODE上查看代码片 v:shapes="_x0000_i1031"><img border="0" width="12" height="12" src="file:///C:/Users/yeshuai/AppData/Local/Temp/msohtmlclip1/01/clip_image001.gif" <="" span="">alt=派生到我的代码片 v:shapes="_x0000_i1032">

1.  strings /usr/lib/libstdc++.so.6 | grep GLIBCXX  
现实如下:

[plain] view plain copy

 <img border="0" width="12" height="12" src="file:///C:/Users/yeshuai/AppData/Local/Temp/msohtmlclip1/01/clip_image001.gif" <="" span="">style='orphans: auto;text-align:start;widows: 1;-webkit-text-stroke-width: 0px;word-spacing:0px' alt=在CODE上查看代码片 v:shapes="_x0000_i1033">

1.  GLIBCXX_3.4  

2. GLIBCXX_3.4.1  

3.  GLIBCXX_3.4.2  

4. GLIBCXX_3.4.3  

5.  GLIBCXX_3.4.4  

6. GLIBCXX_3.4.5  

7.  GLIBCXX_3.4.6  

8. GLIBCXX_3.4.7  

9.  GLIBCXX_3.4.8  

10.GLIBCXX_3.4.9  

11. GLIBCXX_3.4.10  

12.GLIBCXX_3.4.11  

13. GLIBCXX_3.4.12  

14.GLIBCXX_3.4.13  

15. GLIBCXX_3.4.14  

16.GLIBCXX_3.4.15  

17. GLIBCXX_3.4.16  

18.GLIBCXX_3.4.17  

19. GLIBCXX_DEBUG_MESSAGE_LENGTH  


现在已经有了3.4.14版本,在执行一边程序,可以运行了。   

end

 本文转载并整理自博客:

https://www.douban.com/note/543322671/

http://my.oschina.net/vaero/blog/210485    安装

http://blog.csdn.net/xiaolong2w/article/details/23915171    库文件找不到

http://www.ithov.com/linux/130504.shtml     下载64位的libstdc++.so.6


  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值