CentOS 7.9 自带的GCC版本为4.8.5,属于远古版本
将GCC版本到GCC13版本,既然要升级就直接升级到目前的最新版本~
下载站点:
官方站点 https://www.gnu.org/
阿里云镜像站 https://developer.aliyun.com/mirror/
国内使用阿里云镜像站或者其他镜像站下载会快一点
如果GCC编译的过程报错(如果系统内有安装以下三个库则直接跳到GCC编译环节)
configure: error: Building GCC requires GMP 4.2+, MPFR 3.1.0+ and MPC 0.8.0+.Try the –with-gmp, –with-mpfr and/or –with-mpc options to specify their locations.
说明系统缺少GMP、MPFR、MPC三个库
编译GCC的前提需要这三个库
下载GMP
wget https://mirrors.aliyun.com/gnu/gmp/gmp-6.3.0.tar.gz
下载MPFR
wget https://mirrors.aliyun.com/gnu/mpfr/mpfr-4.2.1.tar.gz
下载MPC
wget https://mirrors.aliyun.com/gnu/mpc/mpc-1.3.1.tar.gz
注意:编译顺序为GMP -> MPFR -> MPC
在系统根目录下新建一个soft文件夹,文件夹名字随意,只是后面要用到这个目录作为prefix参数的值
编译GMP
解压
tar -zxvf gmp-6.3.0.tar.gz && cd gmp-6.3.0
编译(用当前目录作为perfix的值)
./configure --prefix=$(pwd) && make && make install
编译MPFR
解压
tar -zxvf mpfr-4.2.1.tar.gz && cd mpfr-4.2.1
编译(这里要用到gmp的目录)
./configure --prefix=$(pwd) \ --with-gmp-include=/soft/gmp-6.3.0/include \ --with-gmp-lib=/soft/gmp-6.3.0/lib \ && make \ && make install
编译MPC
解压
tar -zxvf mpc-1.3.1.tar.gz && cd mpc-1.3.1
编译(这里用到gmp和mpfr的目录)
./configure --prefix=$(pwd) \ --with-gmp-include=/soft/gmp-6.3.0/include \ --with-gmp-lib=/soft/gmp-6.3.0/lib \ --with-mpfr-include=/soft/mpfr-4.2.1/include \ --with-mpfr-lib=/soft/mpfr-4.2.1/lib \ && make \ && make install
配置动态库 (so)到 LD_LIBRARY_PATH 环境变量
永久生效则需要在环境变量中去添加
vi /etc/profile
添加如下内容
export LD_LIBRARY_PATH=/soft/gmp-6.3.0/lib:/soft/mpfr-4.2.1/lib:/soft/mpc-1.3.1/lib
保存文件后让其生效
source /etc/profile
下载GCC
wget https://mirrors.aliyun.com/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz
解压文件
tar -zxvf gcc-13.2.0.tar.gz && cd gcc-13.2.0
编译(这里用到了三个库的路径)
编译gcc需要一定的时间,尤其是在较慢的机器上,可能需要几个小时,需要耐心等待
./configure --disable-multilib \ --prefix=$(pwd)/ \ --with-gmp-include=/soft/gmp-6.3.0/include \ --with-gmp-lib=/soft/gmp-6.3.0/lib \ --with-mpfr-include=/soft/mpfr-4.2.1/include \ --with-mpfr-lib=/soft/mpfr-4.2.1/lib \ --with-mpc-include=/soft/mpc-1.3.1/include \ --with-mpc-lib=/soft/mpc-1.3.1/lib \ && make -j4 \ && make install
将新编译的GCC添加到环境变量中
vi /etc/profile
添加如下内容
export PATH=/soft/gcc-13.2.0/bin:$PATH export LIBRARY_PATH=/soft/gcc-13.2.0/lib64:$LIBRARY_PATH export LD_LIBRARY_PATH=/soft/gcc-13.2.0/lib64:$LD_LIBRARY_PATH export C_INCLUDE_PATH=/soft/gcc-13.2.0/include:$C_INCLUDE_PATH export CPLUS_INCLUDE_PATH=/soft/gcc-13.2.0/include:$CPLUS_INCLUDE_PATH
使其生效
source /etc/profile
通过软链的方式替换老版本GCC
原版本备份(备份是为了保险)
mv /usr/bin/gcc /usr/bin/gcc4.8.5_bak mv /usr/bin/g++ /usr/bin/g++4.8.5_bak mv /usr/bin/c++ /usr/bin/c++4.8.5_bak mv /usr/bin/cpp /usr/bin/cpp4.8.5_bak mv /usr/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so.6_bak mv /usr/lib64/libstdc++.so.6.0.19 /usr/lib64/libstdc++.so.6.0.19_bak
把新版GCC做软连接
ln -s /soft/gcc-13.2.0/bin/gcc /usr/bin/gcc ln -s /soft/gcc-13.2.0/bin/g++ /usr/bin/g++ ln -s /soft/gcc-13.2.0/bin/c++ /usr/bin/c++ ln -s /soft/gcc-13.2.0/bin/cpp /usr/bin/cpp ln -s /soft/gcc-13.2.0/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so.6 ln -s /soft/gcc-13.2.0/lib64/libstdc++.so.6.0.32 /usr/lib64/libstdc++.so.6.0.32
在命令行查看是否已经成功使用新版GCC
gcc --version