我们在RPM 仓库中,提供 CentOS 7.9 的二进制 RPM 安装包最高只到 4.8.5 了,这个和 yum intall gcc 能安装的最高版本一致,没有再高的了。
如果我们想安装更高的GCC 版本,怎么办,只能手工编译源码来做了。
gcc 在 github 上没有,它放在 gnu 自己的仓库中了,https://gcc.gng.org 可以下载,或者使用 git 直接 clone
git clone https://gcc.gnu.org/git/gcc.git
下载后用 git tag 显示下都有哪些版本,切换到你想要安装的版本
然后开始编译 makefile
./configure --host=x86_64-pc-linux-gnu
如果你没有安装另外三个组件,可能会报下面的错:
configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations. Source code for these libraries can be found at
their respective hosting sites as well as at
ftp://gcc.gnu.org/pub/gcc/infrastructure/. See also
http://gcc.gnu.org/install/prerequisites.html for additional info. If
you obtained GMP, MPFR and/or MPC from a vendor distribution package,
make sure that you have installed both the libraries and the header
那你就需要安装他们,这三个之间是有先后安装顺序的,不要搞乱:
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz
下载这三个组件依次安装,如果没有 bzip2 的用 yum install bzip2 安装上,然后
bzip2 -d 文件名 去解压即可
如果你没有安装 m4 ,在 ./configure 时候会报错, yum install m4 即可。
都安装好后就可以顺利编译安装 gcc 了。
如果上面源码安装总是有错,可以提供另外一个通过开发包整体安装的安装的方法:
yum install centos-release-scl
yum install devtoolset-8-gcc*
这个安装完的 GCC 应该是 8.4的,如果想要 GCC 是9的,安装
yum install devtoolset-9-gcc* 即可
这个安装包安装完后不会默认修改 /use/bin/ 下的,所以需要手工替换下:
ln -s /opt/rh/devtoolset-9/root/bin/gcc /usr/bin/gcc
ln -s /opt/rh/devtoolset-9/root/bin/g++ /usr/bin/g++