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
相关推荐
charlie11451419128 分钟前
内核深入学习3——分析ARM32和ARM64体系架构下的Linux内存区域示意图与页表的建立流程
linux·学习·架构·内存管理
Caron_xcb1 小时前
大数据——解决Matplotlib 字体不足问题(Linux\mac\windows)
大数据·linux·matplotlib
水水沝淼㵘1 小时前
嵌入式开发学习日志(数据结构--顺序结构单链表)Day19
linux·服务器·c语言·数据结构·学习·算法·排序算法
愚润求学2 小时前
【Linux】基础 IO(一)
linux·运维·服务器·开发语言·c++·笔记
大白的编程日记.2 小时前
【Linux学习笔记】理解一切皆文件实现原理和文件缓冲区
linux·笔记·学习
孞㐑¥2 小时前
Linux之进程控制
linux·开发语言·c++·经验分享·笔记
愚润求学3 小时前
【Linux】简单设计libc库
linux·运维·开发语言·c++·笔记
chennalC#c.h.JA Ptho3 小时前
Bodhi linux 系统详解
linux·经验分享·笔记·系统架构·系统安全
敲上瘾3 小时前
MySQL数据库表的约束
linux·数据库·sql·mysql·数据库开发·数据库架构·数据库系统
u0109362654 小时前
Linux电源管理(五),发热管理(thermal),温度控制 (结合设备树 和ACPI Source Language(ASL)分析)
linux