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
相关推荐
邪恶的贝利亚2 小时前
FFMEPG常见命令查询
linux·运维·网络·ffmpeg
搜搜秀2 小时前
find指令中使用正则表达式
linux·运维·服务器·正则表达式·bash
弧襪3 小时前
Ubuntu vs CentOS:Shell 环境加载机制差异分析
linux·ubuntu·centos
行思理4 小时前
centos crontab 设置定时任务访问链接
linux·运维·centos
阳光明媚大男孩4 小时前
24.0.2 双系统ubuntu 安装显卡驱动黑屏,系统启动界面键盘失灵
linux·ubuntu·计算机外设
再玩一会儿看代码5 小时前
[特殊字符] 深入理解 WSL2:在 Windows 上运行 Linux 的极致方案
linux·运维·windows·经验分享·笔记·学习方法
良许Linux5 小时前
FreeRTOS大家都是怎么学的呀?
linux
良许Linux5 小时前
类似于STM32之类的MCU,使用RTOS真的比裸机编程有那么大优势?
linux
良许Linux5 小时前
为什么越来越多的人要转行做嵌入式呢?
linux