红联Linux门户
Linux帮助

RedHat AS 4手动编译Gcc 4.4

发布时间:2014-11-07 15:19:19来源:linux网站作者:linux人

Ubuntu用的人都懒了,从来没有编译过GCC什么的东西,一直有sudo权限,所以即使更新神马的,都很容易搞定。现在开始用自强3000服务器,再也不是sudoers,用编译器啥的真叫个要命。折腾了好几天,好歹是弄上了GCC 4.x的c,c++编译器,刚好够自己用。

系统版本:

$ head -n 1 /etc/issue
Red Hat Enterprise Linux AS release 4 (Nahant Update 6)

GCC版本:

$ gcc -v
Reading specs from /usr/lib/gcc/x86_64-RedHat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-9)


安装编译过程:

1. 编译GCC 4.x前提系统条件:

a. GMP 4.3.2以上 (GNU Multiple Precision Library (GMP))

b. MPFR 2.4.2以上

c. MPC 0.8.1Y以上

d. xz (版本不知道,但是需要这个程序)

e. others... (编译的时候,报错后再看系统还缺什么)


2. 编译环境准备:首先编译GMP,然后编译MPFR,最后编译MPC,安装XZ。

a. 下载GMP: http://ftp.tsukuba.wide.ad.jp/software/gmp/gmp-5.0.0.tar.bz2  (选用了小日本的服务器,速度还挺快)

b. 解压并安装GMP

$ bzcat gmp* | tar xvf -

$ cd gmp*

$ ./configure --prefix=$HOME/local        #安装文件路径,为用户下的local文件夹

$ make -j 8             #多核编译参数-j 数字为核数

$ make install

c. 下载MPFR: http://ftp.tsukuba.wide.ad.jp/software/mpfr/mpfr-2.4.2.tar.bz2 (这是至少的版本)

d. 解压并安装MPFR

$ bzcat mpfr* | tar xvf -

$ cd mpfr*

$ ./configure --with-gmp-include=$HOME/local/include --with-gmp-lib=$HOME/local/lib --prefix=$HOME/local          #编译MPFR需要有GMP,因此需要指定GMP的位置,不然就白编译了。

$ make -j 8

$ make install

e. 下载MPC:http://ftp.tsukuba.wide.ad.jp/software/mpc/mpc-0.9.tar.gz

f. 解压并安装MPC

$ tar xzf mpc*

$ cd mpc*

$ ./configure --prefix=$HOME/local --with-gmp=$HOME/local --with-mpfr=$HOME/local

$ make -j 8

$ make install

g. 下载XZ:http://tukaani.org/xz/xz-5.0.3.tar.gz

h. 解压并安装xz

$ tar xzf xz*

$ cd xz*

$ ./configure --prefix=$HOME/local

$ make -j 8

$ make install

i. 配置环境变量

$ vi ~/.bashrc

添加:

export PATH=$HOME/local/bin:$PATH
export LD_LIBRARY_PATH=$HOME/local/lib:$LD_LIBRARY_PATH

j. 更新环境变量

source ~/.bashrc


3. 编译GCC

a. 下载GCC: http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-4.4.3/gcc-4.4.3.tar.bz2 (以GCC 4.4.3为例,后续及之前4.x版本编译方法理论上完全一样)

b. 解压gcc,建立目标目录,我打算将GCC安装至 $HOME/local/gcc目录下,因此:

$ cd $HOME

$ bzcat gcc* | tar xvf -   #假定GCC压缩包在$HOME目录下

$ cd local

$ mkdir gcc

$ cd gcc

$ ../../gcc-4.4.3//configure --prefix=$HOME/local/gcc  --enable-shared --enable-threads=posix --enable-checking --with-zlib=$HOME/local/bin --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --build=x86_64-RedHat-linux --with-gmp=$HOME/local --with-mpfr=$HOME/local --with-mpc=/home/lixuezd/local/ --enable-languages=c,c++      #须指定GMP,MPFR,MPC的位置,编译安装的语言,(单机编译一般是Host,在集群服务器上估计是要用--build,这个不是很明白,没有它编译会找不到ar之类的),详见后文bug-shooting

$ make -j 8

$ make install

m. 配置gcc的环境变量

$ vi ~/.bashrc

添加:

export PATH=$HOME/local/gcc/bin:$PATH
export LD_LIBRARY_PATH=$HOME/local/gcc/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$HOME/local/gcc/lib:$LD_LIBRARY_PATH

保存并更新:

source ~/.bashrc

c. 测试gcc版本

$ gcc -v

Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../../gcc-4.4.3//configure --prefix=/home/lixuezd/local/gcc --enable-shared --enable-threads=posix --enable-checking --with-zlib=/home/lixuezd/local/bin --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --build=x86_64-redhat-linux --with-gmp=/home/lixuezd/local --with-mpfr=/home/lixuezd/local --with-mpc=/home/lixuezd/local/ --enable-languages=c,c++
Thread model: posix
gcc version 4.4.3 (GCC)


4. bug-shooting

a. 采用--host=x86_64-redhat-linux参数,会提示这个错误:

make[2]: x86_64-redhat-linux-ar: Command not found
make[2]: *** [libiberty.a] Error 127
make[2]: Leaving directory `/home/lixuezd/local/gcc/libiberty'
make[1]: *** [all-libiberty] Error 2
make[1]: Leaving directory `/home/lixuezd/local/gcc'
make: *** [all] Error 2
解决方法:改用--build=x86_64-redhat-linux

b. GMP,MPFR,MPC缺失,提示错误忘了记下来了,解决方案是:

依次GMP->MPFR->MPC编译,编译参数层层递进:

GMP: $ ./configure --prefix=$HOME/local

MPFR: $ ./configure --with-gmp-include=$HOME/local/include --with-gmp-lib=$HOME/local/lib --

MPC: $ ./configure --prefix=$HOME/local --with-gmp=$HOME/local --with-mpfr=$HOME/local

c. 没有xz,提示同样没记下来,大致是找不到xz或lzma之类的,解决方案:

安装xz,并添加它的PATH到环境变量中

确认有没有xz,在终端下 输入xz即可,正确时显示:

xz: Compressed data cannot be written to a terminal
xz: Try `xz --help' for more information.

d. ./configure没有更新造成的

configure: error: `CC' has changed since the previous run:
configure: former value: `x86_64-redhat-linux-gcc'
configure: current value: `/home/lixuezd/local/gcc/./gcc/xgcc -B/home/lixuezd/local/gcc/./gcc/ -B/home/lixuezd/local/gcc/x86_64-redhat-linux/bin/ -B/home/lixuezd/local/gcc/x86_64-redhat-linux/lib/ -isystem /home/lixuezd/local/gcc/x86_64-redhat-linux/include -isystem /home/lixuezd/local/gcc/x86_64-redhat-linux/sys-include'
configure: warning: ignoring whitespace changes in `CFLAGS' since the previous run:
configure: former value: `-g -O2'
configure: current value: `-g -O2 '
configure: error: `build_alias' has changed since the previous run:
configure: former value: `x86_64-unknown-linux-gnu'
configure: current value: `x86_64-redhat-linux'
configure: error: in `/home/lixuezd/local/gcc/fixincludes':
configure: error: changes in the environment can compromise the build
configure: error: run `make distclean' and/or `rm ./config.cache' and start over
make[1]: *** [configure-fixincludes] Error 1
make[1]: Leaving directory `/home/lixuezd/local/gcc'
make: *** [all] Error 2
解决方案:

删掉gcc,重新建立gcc后再执行 ./configure .....,或者执行:make disclean和rm ./config.cache命令,我看着麻烦,还是 rm -fr gcc最简单。

这里要提的就是,如果配置发生了更新,如果没有清空上一次的结果,可能会遇上这个问题。

f. java缺少必要的环境

configure: error: libXtst not found, required by java.awt.Robot
make[1]: *** [configure-target-libjava] Error 1
make[1]: Leaving directory `/home/lixuezd/local/gcc'
make: *** [all] Error 2
解决方案:要么去寻找还缺啥,补上它再去编译。因为我不用java,所以我去掉了它的编译:

--enable-languages=c,c++,(java去掉)

g. 64位需要两个Lib,所以添加环境变量时要用:
export LD_LIBRARY_PATH=$HOME/local/gcc/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$HOME/local/gcc/lib:$LD_LIBRARY_PATH

不然会提示 glbc... libstdc++.so.6....什么的东西缺失吧,这个我也没记下来。