本片文章介绍如何升级gcc,centos7.9 仓库默认的gcc版本为:
4.8.5
bash4.8.5-44) Copyright (C) 2015 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 PURPOSE. ```
一、通过SCL仓库安装
-
安装SCL仓库
由于centos7的scl仓库源已经不在维护了
centos-release-scl
,所以我们使用阿里云的scl
镜像源bashcat > /etc/yum.repos.d/CentOS-SCLo-scl.repo << EOF [centos-sclo-rh] name=CentOS-7 - SCLo rh baseurl=https://mirrors.aliyun.com/centos-vault/centos/7/sclo/x86_64/rh/ enabled=1 gpgcheck=0 EOF
-
选择你要安装的gcc版本
阿里云的
scl
镜像仓库目前gcc的最高版本为gcc11
查看所有gcc版本
bash[root@192 ~]# yum list | grep devtoolset- ...... devtoolset-11.x86_64 11.1-2.el7 centos-sclo-rh devtoolset-11-annobin-annocheck.x86_64 10.38-1.el7 centos-sclo-rh devtoolset-11-annobin-docs.noarch 10.38-1.el7 centos-sclo-rh devtoolset-11-annobin-plugin-gcc.x86_64 10.38-1.el7 centos-sclo-rh devtoolset-11-binutils.x86_64 2.36.1-1.el7.2 centos-sclo-rh devtoolset-11-binutils-devel.x86_64 2.36.1-1.el7.2 centos-sclo-rh devtoolset-11-build.x86_64 11.1-2.el7 centos-sclo-rh devtoolset-11-dwz.x86_64 0.14-2.el7 centos-sclo-rh devtoolset-11-dyninst.x86_64 11.0.0-1.el7 centos-sclo-rh devtoolset-11-dyninst-devel.x86_64 11.0.0-1.el7 centos-sclo-rh devtoolset-11-dyninst-doc.x86_64 11.0.0-1.el7 centos-sclo-rh devtoolset-11-dyninst-static.x86_64 11.0.0-1.el7 centos-sclo-rh devtoolset-11-dyninst-testsuite.x86_64 11.0.0-1.el7 centos-sclo-rh devtoolset-11-elfutils.x86_64 0.185-2.el7 centos-sclo-rh devtoolset-11-elfutils-debuginfod.x86_64 0.185-2.el7 centos-sclo-rh devtoolset-11-elfutils-debuginfod-client.x86_64 devtoolset-11-elfutils-debuginfod-client-devel.x86_64 devtoolset-11-elfutils-devel.x86_64 0.185-2.el7 centos-sclo-rh devtoolset-11-elfutils-libelf.x86_64 0.185-2.el7 centos-sclo-rh devtoolset-11-elfutils-libelf-devel.x86_64 0.185-2.el7 centos-sclo-rh devtoolset-11-elfutils-libs.x86_64 0.185-2.el7 centos-sclo-rh devtoolset-11-gcc.x86_64 11.2.1-9.1.el7 centos-sclo-rh devtoolset-11-gcc-c++.x86_64 11.2.1-9.1.el7 centos-sclo-rh devtoolset-11-gcc-gdb-plugin.x86_64 11.2.1-9.1.el7 centos-sclo-rh devtoolset-11-gcc-gfortran.x86_64 11.2.1-9.1.el7 centos-sclo-rh devtoolset-11-gcc-plugin-devel.x86_64 11.2.1-9.1.el7 centos-sclo-rh devtoolset-11-gdb.x86_64 10.2-6.el7 centos-sclo-rh devtoolset-11-gdb-doc.noarch 10.2-6.el7 centos-sclo-rh devtoolset-11-gdb-gdbserver.x86_64 10.2-6.el7 centos-sclo-rh devtoolset-11-libasan-devel.x86_64 11.2.1-9.1.el7 centos-sclo-rh devtoolset-11-libatomic-devel.x86_64 11.2.1-9.1.el7 centos-sclo-rh devtoolset-11-libgccjit.x86_64 11.2.1-9.1 ......
安装gcc11
bash[root@192 ~]# yum -y install devtoolset-11
-
激活 gcc 11 环境
scl enable devtoolset-11 bash
或者source /opt/rh/devtoolset-11/enable
激活gcc11
环境bash[root@192 ~]# scl enable devtoolset-11 bash [root@192 ~]# gcc --version gcc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9) Copyright (C) 2021 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 PURPOSE.
每安装一个
gcc
版本就会在/opt/rh
多一个对应版本的文件,如下bash[root@192 ~]# ls -l /opt/rh total 0 dr-xr-xr-x 3 root root 32 Apr 11 08:46 devtoolset-11 dr-xr-xr-x 3 root root 32 Apr 11 08:58 devtoolset-9
想要切换到那个版本就执行一下对应目录文件下的
enable
脚本文件或者scl enable devtoolset-xx bash
即可bash[root@192 ~]# scl enable devtoolset-9 bash [root@192 ~]# gcc --version gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2) Copyright (C) 2019 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 PURPOSE. [root@192 ~]# source /opt/rh/devtoolset-11/enable [root@192 ~]# gcc --version gcc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9) Copyright (C) 2021 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 PURPOSE.
-
永久切换
每次激活安装的
gcc
时,只不过是在新的bash或者当前bash中追加了gcc环境,并且有新的根文件,可以从enable
文件中体现出来。bash[root@192 ~]# cat /opt/rh/devtoolset-11/enable # General environment variables export PATH=/opt/rh/devtoolset-11/root/usr/bin${PATH:+:${PATH}} export MANPATH=/opt/rh/devtoolset-11/root/usr/share/man${MANPATH:+:${MANPATH}} export INFOPATH=/opt/rh/devtoolset-11/root/usr/share/info${INFOPATH:+:${INFOPATH}} export PCP_DIR=/opt/rh/devtoolset-11/root # bz847911 workaround: # we need to evaluate rpm's installed run-time % { _libdir }, not rpmbuild time # or else /etc/ld.so.conf.d files? rpmlibdir=$(rpm --eval "%{_libdir}") # bz1017604: On 64-bit hosts, we should include also the 32-bit library path. # bz1873882: On 32-bit hosts, we should include also the 64-bit library path. # bz2027377: Avoid unbound variables if [ "$rpmlibdir" != "${rpmlibdir/lib64/}" ]; then rpmlibdir32=":/opt/rh/devtoolset-11/root${rpmlibdir/lib64/lib}" dynpath32="$rpmlibdir32/dyninst" rpmlibdir64= dynpath64= else rpmlibdir64=":/opt/rh/devtoolset-11/root${rpmlibdir/lib/lib64}" dynpath64="$rpmlibdir64/dyninst" rpmlibdir32= dynpath32= fi # Add SCL dyninst to LD_LIBRARY_PATH, both 64- and 32-bit paths. export LD_LIBRARY_PATH=/opt/rh/devtoolset-11/root$rpmlibdir/dyninst$dynpath64$dynpath32${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} # Now prepend the usual /opt/.../usr/lib{64,}. export LD_LIBRARY_PATH=/opt/rh/devtoolset-11/root$rpmlibdir$rpmlibdir64$rpmlibdir32${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} export PKG_CONFIG_PATH=/opt/rh/devtoolset-11/root/usr/lib64/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
在
/opt/rh/devtoolset-11/
目录下存在一个root目录,gcc二进制文件在就/opt/rh/devtoolset-11/root/bin/
目录下面bash[root@192 ~]# ls -l /opt/rh/devtoolset-11/ total 4 -rwxr-xr-x 1 root root 1456 May 30 2022 enable dr-xr-xr-x 17 root root 224 Apr 11 08:46 root [root@192 root]# /opt/rh/devtoolset-11/root/bin/gcc --version gcc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9) Copyright (C) 2021 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 PURPOSE. [root@192 root]# /opt/rh/devtoolset-11/root/bin/g++ --version g++ (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9) Copyright (C) 2021 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 PURPOSE.
了解到了是如何切换到gcc11就能知道如何永久切换到gcc11
方法一、 将
scl enable devtoolset-11 bash
或者source /opt/rh/devtoolset-11/enable
添加到环境变量bashecho "scl enable devtoolset-11 bash" >> /etc/profile
方法二、将devtoolset-11下的gcc 和 g++ 文件链接到
/opt/rh/devtoolset-11/root/bin/gcc
和/opt/rh/devtoolset-11/root/bin/g++
bashmv /usr/bin/gcc /usr/bin/gcc-4.8.5 mv /usr/bin/g++ /usr/bin/g++-4.8.5 ln -s /opt/rh/devtoolset-11/root/bin/gcc /usr/bin/gcc ln -s /opt/rh/devtoolset-11/root/bin/g++ /usr/bin/g++ gcc --version g++ --version