说明:
GCC/GCC-C++编译器用于编译C/C++语言程序,在编译安装工具时需要用到高版本的gcc版本,可以根据需求安装多版本的gcc并使用Modules去管理,不要盲目升级后直接替换gcc。
查看当前系统gcc版本
[root@cs01 ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
[root@cs01 ~]# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
下载centos-release-scl源,安装devtoolset包
[root@cs01 ~]# yum install centos-release-scl scl-utils-build -y
查看源中devtoolset版本信息
[root@cs01 ~]# yum list dev\*gcc
Loaded plugins: fastestmirror, langpacks
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* base: mirrors.bfsu.edu.cn
* centos-sclo-rh: mirrors.bfsu.edu.cn
* centos-sclo-sclo: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.bfsu.edu.cn
Installed Packages
devtoolset-8-gcc.x86_64 8.3.1-3.2.el7 @centos-sclo-rh
Available Packages
devtoolset-10-gcc.x86_64 10.2.1-11.2.el7 centos-sclo-rh
devtoolset-11-annobin-plugin-gcc.x86_64 10.38-1.el7 centos-sclo-rh
devtoolset-11-gcc.x86_64 11.2.1-9.1.el7 centos-sclo-rh
devtoolset-7-gcc.x86_64 7.3.1-5.16.el7 centos-sclo-rh
devtoolset-9-gcc.x86_64
devtoolset对应gcc的版本
devtoolset-3 对应 gcc4.x.x版本
devtoolset-4 对应 gcc5.x.x版本
devtoolset-6 对应 gcc6.x.x版本
devtoolset-7 对应 gcc7.x.x版本
devtoolset-8 对应 gcc8.x.x版本
devtoolset-9 对应 gcc9.x.x版本
devtoolset-10 对应 gcc10.x.x版本
devtoolset-11 对应 gcc11.x.x版本
安装devtoolset
[root@cs01 ~]# yum install devtoolset-8
切换环境变量
[root@cs01 ~]# source /opt/rh/devtoolset-8/enable
源码编译安装gcc-8.4.0
yum -y install bzip2 bzip2-devel gmp-devel mpfr-devel libmpc-devel gettext-devel texinfo # 安装依赖
wget http://ftp.gnu.org/gnu/gcc/gcc-8.4.0/gcc-8.4.0.tar.gz
tar -zxvf gcc-8.4.0.tar.gz
cd gcc-8.4.0
下载依赖包
./contrib/download_prerequisites
# gmp-6.1.0.tar.bz2: OK
# mpfr-3.1.4.tar.bz2: OK
# mpc-1.0.3.tar.gz: OK
# isl-0.18.tar.bz2: OK
# All prerequisites downloaded successfully.
创建编译目录,编译安装
mkdir build
cd build
../configure --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --enable-gnu-indirect-function --with-tune=generic --disable-multilib --prefix=/usr/local/gcc-8.4.0
make -j`nproc`
make install
配置环境变量
echo "export PATH=/usr/local/gcc-8.4/bin:$PATH" >> /etc/profile.d/gcc-8.4.sh
source /etc/profile.d/gcc-8.4.sh
ln -sv /usr/local/gcc-8.4/include/ /usr/include/gcc # 配置头文件
echo "/usr/local/gcc-8.4/lib64" >> /etc/ld.so.conf.d/gcc-8.4.conf # 配置库文件
ldconfig -v
ldconfig -p |grep gcc
小结:
联网环境下使用yum源升级更简单,源码编译安装如果机器可以联网会简单些,如果是内网环境需要手动下载依赖上传至服务器,安装完成后不建议直接替换原系统gcc版本,可以配置环境变量根据需求取source这样更灵活。