源码编译安装gcc-5.3.0

时间:2022-04-06 11:15:30

系统环境:Amazon Linux AMI 2015.09.2 (HVM)---Fedora 23 Server

1、下载gcc-5.3.0安装包并将gcc-5.3.0.tar.gz放到/opt目录下解压:

#sudo su

#mkdir /opt/&cd /opt/

#wget http://ftp.gnu.org/gnu/gcc/gcc-5.3.0/gcc-5.3.0.tar.gz

#tar –zxf gcc-5.3.0.tar.gz

2、创建安装目录:

#mkdir /usr/local/gcc-5.3.0/

#cd /usr/local/gcc-5.3.0/

3、配置安装文件:

#/opt/gcc-5.3.0/configure –prefix=/usr/local/gcc-5.3.0

如果执行时出现如下报错:

-----------------------------------------------------------------------

configure: error: in `/usr/local/gcc-5.3.0':
configure: error: no acceptable C
compiler found in $PATH

See `config.log' for more details.

-----------------------------------------------------------------------

说明$PATH里没有找到C的编译器
如果系统有gcc编译器,那就设置下环境变量
比如:
export
PATH=$PATH:/usr/local/bin

如果没有找到那么先安装gcc低版本的rpm包

安装顺序:cpp-kernel--->headers--->glibc--->headers--->glibc--->devel--->libgomp--->gcc

重复步骤3,又出现相关报错:

-------------------------------------------------------------------------------------------------

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.

-------------------------------------------------------------------------------------------------

错误说明:要安装gcc需要GMP、MPFR、MPC这三个库,可从ftp://gcc.gnu.org/pub/gcc/infrastructure/下载相应的压缩包。由于MPFR依赖GMP,而MPC依赖GMP和MPFR,所以要先安装GMP,其次MPFR,最后才是MPC。

①.安装gmp4.3.2

#tar jxvf gmp-4.3.2.tar.bz2

#mkdir /usr/local/gmp-4.3.2

#cd /usr/local/gmp-4.3.2

#/opt/gmp-4.3.2/configure –prefix=/usr/local/gmp-4.3.2

#make

make install

②安装mpfr2.4.2

#tar jxvf mpfr-2.4.2.tar.bz2

#mkdir /usr/local/mpfr-2.4.2

#cd /usr/local/mpfr-2.4.2

#/opt/mpfr-2.4.2/configure --prefix=/usr/local/mpfr-2.4.2
--with-gmp=/usr/local/gmp-4.3.2
#make

#make install

③安装mpc0.8.1

#tar –zxf mpc-0.8.1.tar.gz

#mkdir /usr/local/mpc-0.8.1

#cd /usr/local/mpc-0.8.1

#/opt/mpc-0.8.1/configure --prefix=/usr/local/mpc-0.8.1
--with-gmp=/usr/local/gmp-4.3.2 --with-mpfr=/usr/local/mpfr-2.4.2

#make

#make install

④再次安装GCC 配置安装选项

#cd /usr/local/gcc-5.3.0

#/opt/gcc-5.3.0/configure --prefix=/usr/local/gcc-5.3.0 -enable-threads=posix
-disable-checking -disable-multilib -enable-languages=c,c++
--with-gmp=/usr/local/gmp-4.3.2 --with-mpfr=/usr/local/mpfr-2.4.2
--with-mpc=/usr/local/mpc-0.8.1

4、编译安装文件

#make

再次报错:

/usr/local/gcc-5.3.0/./prev-gcc/cc1plus: error while loading shared
libraries: libmpc.so.2: cannot open shared object file: No such file or
directory
make[3]: *** [real.o] Error 1
make[3]: Leaving directory
`/usr/local/gcc-5.3.0/gcc'
make[2]: *** [all-stage3-gcc] Error 2

make[2]: Leaving directory `/usr/local/gcc-5.3.0'
make[1]: ***
[stage3-bubble] Error 2
make[1]: Leaving directory `/usr/local/gcc-5.3.0'

make: *** [all] Error 2

分析:/usr/local/gcc-5.3.0/x86_64-unknown-linux-gnu/libgcc/config.log

查找下error会发现很多错误,大部分是库没找到。

#export
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc-0.8.1/lib:/usr/local/gmp-4.3.2/lib:/usr/local/mpfr-2.4.2/lib

#make

大概30 min后···
终于编译成功了。

libtool: link: ranlib .libs/libatomic.a
libtool: link: ( cd ".libs"
&& rm -f "libatomic.la" && ln -s "../libatomic.la"
"libatomic.la" )
true  DO=all multi-do # make
make[4]: Leaving directory
`/usr/local/gcc-5.3.0/x86_64-unknown-linux-gnu/libatomic'
make[3]: Leaving
directory `/usr/local/gcc-5.3.0/x86_64-unknown-linux-gnu/libatomic'
make[2]:
Leaving directory `/usr/local/gcc-5.3.0/x86_64-unknown-linux-gnu/libatomic'

make[1]: Leaving directory `/usr/local/gcc-5.3.0'

5、执行安装gcc:

#make install

6、将新版本的gcc加入命令搜索路径中:

#ln -s /usr/local/gcc-5.3.0/bin/gcc gcc
#ln -s
/usr/local/gcc-5.3.0/bin/g++ g++

7、添加环境变量:

# vi /etc/bashrc

加入代码:

添加如下代码:

# gcc

LD_LIBRARY_PATH=:/usr/local/mpc-0.8.1/lib:/usr/local/gmp-4.3.2/lib:/usr/local/mpfr-2.4.2/lib:/usr/local/gcc-4.6.1/lib

#export LD_LIBRARY_PATH

至此安装完毕!