gcc的安装与多版本gcc切换

#注:
本文仅适用于安装gcc-6.3 g+±6.3之类的,因为apt-get不支持安装gcc-5以上多版本。如需安装版本5之下的,请直接使用apt-get 安装。参考别人链接:https://blog.csdn.net/zhangxin4832/article/details/79225394/ 。对这个链接里内容不解也可留言~

因为需要配置环境测试实验,需要更换gcc测试哪个版本合适。所以有了此文。欢迎交流。
#序:GCC依赖于gmp 4.2+, mpfr 2.4+和mpc 0.8+,并且安装顺序不可以改变。这里直接下载安装最新的版本。总的说就是每一步都是

configure(区别最大)
make
sudo make install

这篇文章跟我思路很类似,出现错误可以参考
https://blog.csdn.net/anneqiqi/article/details/51725658
各版本gcc下载:http://ftp.gnu.org/gnu/gcc/
#前期工作:因为反正最后都要添加环境变量,那么我们就第一步完成吧,以免后面报错。
打开profile:sudo gedit /etc/profile
在文本末尾添加:(export后面没有回车)

#gcc
export
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc/lib:/usr/local/gmp/lib:/usr/local/mpfr/lib:/usr/local/gcc/lib

#1、安装gmp 6.1.2
wget后面对应的是下载地址,其他版本的阶段到gmp处即可以

wget https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz
tar xvf gmp-6.1.2.tar.xz
cd gmp-6.1.2
./configure --prefix=/usr/local/gmp
make -j20 && make install

gcc的安装与多版本gcc切换

其中:configure --prefix=是指的安装目录,后文还有–with指的是依赖文件的目录。prefix和with前面都是两个-
configure 、make、make install是linux中安装文件常见的三步骤

gcc的安装与多版本gcc切换

gcc的安装与多版本gcc切换
gcc的安装与多版本gcc切换

执行./configure –prefix=/usr/local/gmp 可能会出现如下错误

checking whether sscanf needs writable input… no
checking for struct pst_processor.psp_iticksperclktick… no
checking for suitable m4… configure: error: No usable m4 in $PATH or /usr/5bin (see config.log for reasons).

那就安装m4

yum install m4

安装成功后再次执行configure命令成功会显示如下界面

config.status: linking mpn/x86_64/coreihwl/gmp-mparam.h to gmp-mparam.h
config.status: executing libtool commands
configure: summary of build options:

Version: GNU MP 6.1.2
Host type: haswell-pc-linux-gnu
ABI: 64
Install prefix: /usr/local/gmp
Compiler: gcc -std=gnu99
Static libraries: yes
Shared libraries: yes

#2:安装mpfr 3.1.5 mpfr依赖于gmp

wget http://www.mpfr.org/mpfr-current/mpfr-3.1.5.tar.gz
tar xvf mpfr-3.1.5.tar.gz
cd mpfr-3.1.5
./configure --prefix=/usr/local/mpfr --with-gmp=/usr/local/gmp
make -j20 && make install

gcc的安装与多版本gcc切换
gcc的安装与多版本gcc切换
gcc的安装与多版本gcc切换

#3:安装mpc 1.1.0 mpc依赖于gmp和mpfr

wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz
tar xvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure --prefix=/usr/local/mpc --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr

make  
sudo make install

gcc的安装与多版本gcc切换

gcc的安装与多版本gcc切换
gcc的安装与多版本gcc切换

gcc的安装与多版本gcc切换
gcc的安装与多版本gcc切换

#4:安装GCC 6.3.0

wget ftp://ftp.gnu.org/gnu/gcc/gcc-6.3.0/gcc-6.3.0.tar.gz
tar xvf gcc-6.3.0.tar.gz
cd gcc-6.3.0
sudo ./configure --prefix=/usr/local/gcc-6.3 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr --with-mpc=/usr/local/mpc
#这步make时间比较长,很长~
make 
make install

#软链接一下,其实这里软链接完全都是为了支持多版本gcc
sudo ln -s /usr/local/gcc-6.3  /usr/local/gcc
sudo ln -s /usr/local/gcc/bin/  /usr/bin/
sudo ln -s /usr/local/gcc/bin/g++  /usr/bin/

gcc的安装与多版本gcc切换
#5:gcc版本切换
安装其他版本只需要重新进行第4步就可以,因为我觉得反正用的时候只用一种gcc,那么gcc依赖完全可以共用一份,如果依赖版本有异那么从1开始装就可以。注意目录就可以
#完
安装过程中可能会出现:

“checking for suffix of object files… configure: error: cannot compute suffix of object files: cannot compile
See `config.log’ for more details.
make[2]: * [configure-stage1-target-libgcc] Error 1
make[2]: Leaving directory `/tmp/gcc-6.3.0’
make[1]: * [stage1-bubble] Error 2
make[1]: Leaving directory `/tmp/gcc-6.3.0’
make: * [bootstrap] Error 2

解决方法:
sudo apt-get install m4
sudo apt-get install gcc-c++

实际解决办法:编辑变量,把我们安装的gmp,mpfr,mpc加进去
vi /etc/ld.so .conf

添加部分:#这个是默认系统的变量
/usr/local/lib
/usr/local/gmp/lib
/usr/local/mpfr/lib
/usr/local/mpc/lib
添加保存后记得更新动态库的缓存:
sudo ldconfig -v

更新后再去重新编译安装。

备份系统默认的gcc版本
sudo mv /usr/bin/gcc /usr/bin/gcc-bak
sudo mv /usr/bin/g++ /usr/bin/g+±bak
sudo mv /usr/bin/c++ /usr/bin/c+±bak

创建新的gcc软连接
sudo ln -sf /usr/local/gcc/bin/gcc /usr/bin/gcc
sudo ln -sf /usr/local/gcc/bin/c++ /usr/bin/c++
sudo ln -sf /usr/local/gcc/bin/g++ /usr/bin/g++
sudo ln -sf /usr/local/gcc/lib64/libstdc++.so.6.0.22 /usr/lib64/libstdc++.so.6

#查看gcc版本:
[[email protected] ~]# gcc --version
gcc (GCC) 6.3.0
Copyright © 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR

[[email protected] ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc/libexec/gcc/x86_64-pc-linux-gnu/6.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure –prefix=/usr/local/gcc –enable-threads=posix –disable-checking –disable-multilib –enable-languages=c,c++ –with-gmp=/usr/local/gmp –with-mpfr=/usr/local/mpfr –with-mpc=/usr/local/mpc
Thread model: posix
gcc version 6.3.0 (GCC)

参考:https://blog.csdn.net/u014608280/article/details/80569328

#后记:
可能因为我返回安装的原因,可以参考此文先卸载后安装老版本4.77,再重新安装新版本(gcc和g++同步装)
https://blog.csdn.net/ppp036/article/details/51126468

有问题欢迎提问~

sudo ln -sri /usr/local/gcc/bin/* /usr/local/bin