centos7.9 升级 gcc

本片文章介绍如何升级gcc,centos7.9 仓库默认的gcc版本为:4.8.5

bash 复制代码
4.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仓库安装

  1. 安装SCL仓库

    由于centos7的scl仓库源已经不在维护了 centos-release-scl,所以我们使用阿里云的scl镜像源

    bash 复制代码
    cat > /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
  2. 选择你要安装的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
  3. 激活 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.
  4. 永久切换

    每次激活安装的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 添加到环境变量

    bash 复制代码
    echo "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++

    bash 复制代码
    mv /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
相关推荐
A小辣椒2 天前
TShark:Wireshark CLI 功能
linux
A小辣椒2 天前
TShark:基础知识
linux
AlfredZhao2 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao2 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334663 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪3 天前
linux 拷贝文件或目录到指定的位置
linux
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
bush43 天前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5203 天前
Linux 11 动态监控指令top
linux
不会C语言的男孩3 天前
Linux 系统编程 · 第 8 章:进程基础
linux·c语言