从svn checkout svn://gcc.gnu.org/svn/gcc/trunk拿了GCC的最新代码,打算编译了学东西习学习C++ 11的东西,结果在configure的时候出现如下问题:
Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+
http://www.multiprecision.org/mpc 下载mpc-1.0.2.tar.gz
ftp://ftp.gnu.org/gnu/gmp/gmp-5.0.1.tar.bz2下载gmp-5.0.1.tar.bz2
http://ftp.gnu.org/gnu/mpfr/下载mpfr-3.1.2.tar.xz。
先开始安装GMP。解压GMP的压缩包后,得到源代码目录gmp-5.0.1。在该目录的同级目录下建立一个临时的编译目录,这里命名为temp。然后开始配置安装选项,进入temp目录,输入以下命令进行配置:
../gmp-5.0.1/configure --prefix=/usr/local/gmp-5.0.1
make
make install
mpfr和mpc的安装方法与gmp类似。不过要注意配置的时候要把gmp与mpfr的依赖关系选项加进去,具体配置命令如下:
../mpfr-3.1.2/configure --prefix=/usr/local/mpfr-3.1.2 --with-gmp=/usr/local/gmp-5.0.1
../mpc-1.0.2/configure --prefix=/usr/local/mpc-1.0.2--with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.2
安装好这三个库之后,就可以正式开始安装gcc了。
当然了链接的时候,需要刚刚编译的3个lib。
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc-1.0.2/lib:/usr/local/gmp-5.0.1/lib:/usr/local/mpfr-3.1.2/lib
然后是典型的configure,make,install三步曲。
../configure --prefix=/usr/local/gcc-4.9 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++
--with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.2 --with-mpc=/usr/local/mpc-1.0.2
make
make check(可选)
make install
经过漫长的等待:
/usr/local/gcc-4.9/bin/g++ -v
使用内建 specs。
COLLECT_GCC=/usr/local/gcc-4.9/bin/g++
COLLECT_LTO_WRAPPER=/usr/local/gcc-4.9/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper
目标:x86_64-unknown-linux-gnu
配置为:../configure --prefix=/usr/local/gcc-4.9 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.2 --with-mpc=/usr/local/mpc-1.0.2
线程模型:posix
gcc 版本 4.9.0 (GCC)
开始C++ 11学习了。
下面是另外一位朋友博客:
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....什么的东西缺失吧,这个我也没记下来
转载于:https://blog.51cto.com/655522/1418645