系统版本:ubuntu16.04 gcc默认版本为5.4,业务需要升级gcc7+
安装路径:/usr/local/lib/
源码安装包及依赖包下载
- 下载gcc7.3
gcc7.3源码包地址下载:https://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-7.3.0/gcc-7.3.0.tar.gz ## 其他可选版本https://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/
##如果直接解压编译安装或报错
tar -xf gcc-7.3.0.tar.gz
cd gcc-7.3.0/
./configure
##会报异常
=============================出现错误=====================================
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三个软件,所以需要编译安装这三个包,同时gmp、mpfr、mpc之间相互依赖,所以需要按顺序安装,在configure时指定其路径
-
下载gmp4.3.2
gmp4.3.2源码包地址下载:https://gcc.gnu.org/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2 -
下载mpfr2.4.2
mpfr2.4.2源码包地址下载:https://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2 -
下载mpc0.8.1
mpc0.8.1源码包地址下载:https://gcc.gnu.org/pub/gcc/infrastructure/mpc-0.8.1.tar.gz
安装及错误解决
解压安装gmp
tar -xf gmp-4.3.2.tar.bz2
cd gmp-4.3.2/
./configure --prefix=/usr/local/lib/gmp-4.3.2
make -j48 && make install ### -j 参数根据自己服务器情况设定,因为gcc编译如果单线程编译需要1h+,多线程可以缩短编译时间
解压安装mpfr
tar -jxvf mpfr-2.4.2.tar.bz2
cd mpfr-2.4.2
./configure --prefix=/usr/local/lib/mpfr-2.4.2 --with-gmp=/usr/local/lib/gmp-4.3.2
make -j48 && make install
解压安装mpc
tar -zxvf mpc-0.8.1.tar.gz
cd mpc-0.8.1
./configure --prefix=/usr/local/lib/mpc-0.8.1 --with-gmp=/usr/local/lib/gmp-4.3.2 --with-mpfr=/usr/local/lib/mpfr-2.4.2
make -j48 && make install
压安装gcc
tar -xf gcc-7.3.0.tar.gz
cd gcc-7.3.0
./configure --prefix=/usr/local/lib/gcc7.3 --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/lib/gmp-4.3.2 --with-mpfr=/usr/local/lib/mpfr-2.4.2 --with-mpc=/usr/local/lib/mpc-0.8.1
make -j48 && make install
报错及解决方案
- 报错提示
gcc error while loading shared libraries: libmpc.so.2:
cannot open shared object file:No such file or directory
- 解决方案
vim /etc/profile ##追加如下参数
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/gmp-4.3.2/lib:/usr/local/lib/mpfr-2.4.2/lib:/usr/local/lib/mpc-0.8.1/lib
source /etc/profile
echo $LD_LIBRARY_PATH ##输出如下
:/usr/local/lib/gmp-4.3.2/lib:/usr/local/lib/mpfr-2.4.2/lib:/usr/local/lib/mpc-0.8.1/lib
vim /etc/ld.so.conf ##追加如下参数
/usr/local/lib/mpc-0.8.1/lib
/usr/local/lib/gmp-4.3.2/lib
/usr/local/lib/mpfr-2.4.2/lib
ldconfig