现在的位置: 首页 > 编程语言 > 正文

从源代码编译、安装gcc

2019年04月29日 编程语言 ⁄ 共 2506字 ⁄ 字号 评论关闭
从源代码编译、安装gcc
参考:

http://www.comdyn.cn/from-web/68-server-setup/164-centos-48-gcc450.html

我的环境:CentOS 6.2, kernel 3.1.10  x86_64
需要的配置:Disk space >= 6GB, Mem >= 1GB

从gcc.gnu.org下载gcc-4.7.2.tar.bz2,创建/root/buid-gcc目录,以后所有编译都在这个目录里进行了。
ftp://gcc.gnu.org/pub/gcc/infrastructure/处下载以下辅助安装包:gmp-4.3.2.tar.bz2, mpfr-2.4.2.tar.bz2, mpc-0.8.1.tar.gz

将以上bz2和gz ball全部放到/root/build-gcc目录下。

1. 编译gmp-4.3.2
展开gmp-4.3.2.tar.bz2:
#tar xf gmp-4.3.2.tar.bz2
#mkdir gmp-build
#cd gmp-build
#../gmp-4.3.2/configure --prefix=/root/rpmbuild/gmp-build 
--build=x86_64-linux
注意:
--build=x86_64-linux选项对于x86_64的平台(比如我用的这个系统)非常重要,否则,无法生成Makefile。
生成Makefile以后,用以下命令编译:
#make
#make check
#make install
这样就把gmp安装到了
/root/build-gcc/gmp-build目录,gmp的安装就完成了,/root/build-gcc/gmp-build目录在将来安装其他包的时候会作为参数被传递。

2. 编译mpfr
#tar xf mpfr-2.4.2.tar.bz2
#mkdir mpfr-build
#cd mpfr-build
#
../mpfr-2.4.2/configure --prefix=/root/build-gcc/mpfr-build/ --with-gmp=/root/build-gcc/gmp-build
#make
#make check
#make install

3.编译mpc
#tar xf mpc-0.8.1.tar.gz
#mkdir mpc-build
#cd mpc-build
#../mpc-0.8.1/configure --prefix=/root/build-gcc/mpc-build/ --with-gmp=/root/build-gcc/gmp-build/ --with-mpfr=/root/build-gcc/mpfr-build/
#make; make check; make install

4.编译GCC
#tar xf gcc-4.7.2.tar.bz2
#mkdir gcc-build
设置LD_LIBRARY_PATH (可选)
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/build-gcc/mpc-build/lib:/root/build-gcc/mpfr-build/lib:/root/build-gcc/gmp-build/lib
创建makefile
#../gcc-4.7.2/configure --prefix=/root/build-gcc/gcc-build/ --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/root/build-gcc/gmp-build/ --with-mpfr=/root/build-gcc/mpfr-build/ --with-mpc=/root/build-gcc/mpc-build/

Build过程中可能出现的错误:
有些包没装(虽然之前已经通过命令#yum groupinstall "Development Tools" 安装了"Development Tools"),比如ppl和ppl-devel,可能会出现错误:configure: error: cannot compute suffix of object files: cannot
compile。查看日志发现错误记录:

conftest.c:10:19: error: ppl_c.h: No such file or directory
conftest.c:16: error: 'choke' undeclared (first use in this function)
conftest.c:16: error: (Each undeclared identifier is reported only once
conftest.c:16: error: for each function it appears in.)
conftest.c:16: error: expected ';' before 'me'

这些错误可以通过命令:
#yum install ppl ppl-devel
安装这两个包来改正。

另外,如果严谨一点, make完后应该要做make -k check的,但是make -k check会报告缺少autogen这个命令,可问题在于CentOS里很难找到这个autogen,所以,马虎点好了,make完后直接make install。

make install完成后,将会在/root/build-gcc/gcc-build/bin目录下生成最终的可执行文件,如gcc,g++这些。而这时,/root/build-gcc/gcc-build/bin并不存在于PATH中。这就需要将新生成的gcc放到/usr/bin中,让其“可用”了:
#ln -s /root/build-gcc/gcc-build/bin/gcc /usr/bin/gcc-4.7

#ln -s /root/build-gcc/gcc-build/bin/g++ /usr/bin/g++-4.7
注意:以上必须用绝对路径!

其后就可以用gcc-4.7和g++-4.7命令编程序了 

抱歉!评论已关闭.