• 【香菇带你学Linux】Linux环境下gcc编译安装【建议收藏】


    0. 前言

    gcc(GNU Compiler Collection)是GNU项目的一部分,它是一个支持多种编程语言的编译器集合,但最常用的是作为C和C++的编译器。GCC能够编译、汇编和链接C、C++、Objective-C、Fortran、Ada、Go以及D等多种语言的程序。它因其跨平台性、高效性和灵活性而受到广泛的欢迎和使用。

    我的系统类型规格如下,openeuler属于redhat/centos系列。Ubuntu系列主机本文仅供参考。

    [root@localhost ~]# cat /etc/redhat-release 
    BigCloud Enterprise Linux For Euler release 21.10 (LTS-SP2)
    [root@localhost ~]# cat /etc/os-release 
    NAME="BigCloud Enterprise Linux"
    VERSION="21.10 (LTS-SP2)"
    ID="bclinux"
    VERSION_ID="21.10"
    PRETTY_NAME="BigCloud Enterprise Linux For Euler 21.10 LTS"
    ANSI_COLOR="0;31"
    
    [root@localhost ~]# free -g
                  total        used        free      shared  buff/cache   available
    Mem:             15           0          14           0           0          14
    Swap:             7           0           7
    
    

    最近要在BClinux for openeuler上安装mysql8.0结果各种报错,缺少很多依赖。可惜系统自带的yum源要么就是没有这个安装包,要么就是软件版本不符合要求。所以只能选择源码编译安装。当前系统gcc版本为7.3.0,要升级到10以上.

    安装gcc前需要安装GMP、MPFR、MPC这三个依赖库

    # 查看当前系统gcc版本
    root@localhost ~]# gcc --version
    gcc (GCC) 7.3.0
    Copyright © 2017 Free Software Foundation, Inc.
    本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
    包括没有适销性和某一专用目的下的适用性担保。
    

    1. 安装前准备工作

    如果需要再root下直接安装,请参考文章最后的root下编译安装gcc脚本

    1.1 创建weihu用户

    养成良好安装习惯,不使用root直接安装。如果需要再root下安装,请略过本文1.1小节即可

    这里我们注册一个weihu用户,并赋予维护用户sudo权限。然后使用weihu用户安装

    # 创建weihu用户
    [root@localhost ~]# useradd -m weihu
    # 设置weihu的密码
    [root@localhost ~]# passwd weihu
    更改用户 weihu 的密码 。
    新的 密码:
    重新输入新的 密码:
    passwd:所有的身份验证令牌已经成功更新。
    

    赋予weihu用户sudo权限

    # 编辑 /etc/sudoers文件 ,找到下面的一行内容
    # root    ALL=(ALL)       ALL
    [root@localhost ~]# vi /etc/sudoers
    
    

    image-20240713224548148

    root ALL=(ALL) ALL这一行下面添加

    image-20240713224650697

    保存退出

    接下来的所有操作均在weihu用户下面操作

    # 登录weihu用户
    [root@localhost ~]# su - weihu
    

    1.2 安装依赖包

    编译安装gcc之前,需要安装GMP、MPFR、MPC三个依赖。且三个依赖包的安装顺序由先后。同样,我们也需要分别编译安装(使用yum安装的版本较低,报错较多)

    安装之前,先建个文件夹用于存放源码

    # 将
    [weihu@localhost ~]$ mkdir /home/weihu/soft
    [weihu@localhost ~]$ cd /home/weihu/soft
    
    
    1.2.1 安装 GMP

    CMP下载网址:https://gcc.gnu.org/pub/gcc/infrastructure/

    这里我们下周最新的版本6.2.1版本

    image-20240713225541261

    可以下载到本地再上传到Linux主机,若Linux主机可以访问公网,也可以通过wget方向直接下载到Linux主机。

    这里我选择第二种方法。

    # 下载gmp-6.2.1.tar.bz2源码
    [weihu@localhost ~]$ cd /home/weihu/soft/
    [weihu@localhost soft]$ wget https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
    # 解压gmp-6.2.1.tar.bz2源码
    [weihu@localhost soft]$ tar -xvf gmp-6.2.1.tar.bz2
    # 进入解压后的文件夹
    [weihu@localhost soft]$ cd gmp-6.2.1/
    # #创建并进入安装目录
    [weihu@localhost gmp-6.2.1]$ mkdir build
    [weihu@localhost gmp-6.2.1]$ cd build
    [weihu@localhost build]$ 
    # #配置安装
    [weihu@localhost build]$ ../configure -prefix=/usr/local/gmp-6.2.1
    # 编译
    weihu@localhost build]$ make -j$(nproc)
    # 安装
    [weihu@localhost build]$ sudo make install
    

    这样就把gmp安装在/usr/local/gmp-6.2.1路径下

    在进行编译安装的时候。我用的命令为 make -j$(nproc)

    使用 -j 选项可以指定同时运行的作业(即编译任务)的最大数量。如果 -j 后面跟的是一个数字,那么 make 会尝试同时运行指定数量的作业。如果不跟数字,或者跟的是 0make 会尝试同时运行尽可能多的作业。

    1.2.2 安装MPFR

    MPFR下载网址:https://gcc.gnu.org/pub/gcc/infrastructure/

    本次,我们选择MPFR版本为4.1.0

    # 下载源码
    [weihu@localhost ~]$ cd /home/weihu/soft/
    [weihu@localhost soft]$ wget https://gcc.gnu.org/pub/gcc/infrastructure/mpfr-4.1.0.tar.bz2
    # 解压mpfr-4.1.0.tar.bz2
    [weihu@localhost soft]$ tar -xvf mpfr-4.1.0.tar.bz2 
    # 进入解压后的文件
    [weihu@localhost soft]$ cd mpfr-4.1.0/
    # 新建构建文件夹并进入
    weihu@localhost mpfr-4.1.0]$ mkdir build 
    weihu@localhost mpfr-4.1.0]$  cd build
    # 配置安装
    [weihu@localhost build]$ ../configure --prefix=/usr/local/mpfr-4.1.0 --with-gmp=/usr/local/gmp-6.2.1 
    # 编译
    [weihu@localhost build]$ make -j$(nproc)
    # 安装
    [weihu@localhost build]$ sudo make install
    

    这样就把mpfr-4.1.0安装在/usr/local/mpfr-4.1.0路径下

    1.2.3 安装MPC

    PC下载网址:https://gcc.gnu.org/pub/gcc/infrastructure/

    本次,我们选择MPC版本为1.2.1

    # 下载源码
    [weihu@localhost soft]$ cd /home/weihu/soft
    [weihu@localhost soft]$ wget https://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.2.1.tar.gz
    #解压源码
    [weihu@localhost soft]$ tar -xvf mpc-1.2.1.tar.gz 
    # 进入解压后的文件夹
    [weihu@localhost soft]$ cd mpc-1.2.1/
    # 创建构建文件夹并进入
    [weihu@localhost mpc-1.2.1]$ mkdir build
    [weihu@localhost mpc-1.2.1]$ cd build
    #配置安装
    [weihu@localhost build]$ ../configure --prefix=/usr/local/mpc-1.2.1 --with-gmp=/usr/local/gmp-6.2.1 --with-mpfr=/usr/local/mpfr-4.1.0
    # 编译
    [weihu@localhost build]$ make -j$(nproc)
    # 安装
    [weihu@localhost build]$ sudo make install
    

    这样就把mpc-1.2.1安装在/usr/local/mpc-1.2.1路径下

    2. gcc10.0.1版本安装

    gcc源码下载地址:https://gcc.gnu.org/pub/gcc/releases/

    本次我们选择gcc-10.1.0.tar.gz安装

    # 下载源码
    [weihu@localhost soft]$ cd /home/weihu/soft
    [weihu@localhost soft]$ wget https://gcc.gnu.org/pub/gcc/releases/gcc-10.1.0/gcc-10.1.0.tar.gz
    # 解压源码并进入
    [weihu@localhost soft]$ tar -xvzf gcc-10.1.0.tar.gz 
    [weihu@localhost soft]$ cd gcc-10.1.0/
    # 创建构建文件夹并进入
    [weihu@localhost gcc-10.1.0]$ mkdir build
    [weihu@localhost gcc-10.1.0]$ cd build
    # 配置安装
    [weihu@localhost build]$ ../configure --prefix=/usr/local/gcc-10.1.0 -enable-threads=posix -disable-checking -disable-multilib -enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.2.1 --with-mpfr=/usr/local/mpfr-4.1.0 --with-mpc=/usr/local/mpc-1.2.1
    # 编译(时间较长)
    [weihu@localhost build]$ make -j$(nproc)
    # 安装
    [weihu@localhost build]$ sudo make install
    

    gcc至此安装成功,然后我们将gcc添加进入系统环境变量

    # 软链接
    [weihu@localhost ~]$ sudo ln -s /usr/local/gcc-10.1.0/bin/gcc gcc 
    [weihu@localhost ~]$ sudo ln -s /usr/local/gcc-10.1.0/bin/g++ g++
    [weihu@localhost ~]$ export PATH=/usr/local/gcc-10.1.0/bin:$PATH
    
    

    查看gcc版本

    [weihu@localhost ~]$ gcc --version
    gcc (GCC) 10.1.0
    Copyright (C) 2020 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.
    
    [weihu@localhost ~]$ 
    
    

    3. 报错解决

    3. 1. wget下载报错

    [weihu@localhost soft]$ wget https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
    --2024-03-28 00:17:13--  https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
    Resolving gcc.gnu.org (gcc.gnu.org)... 8.43.85.97, 2620:52:3:1:0:246e:9693:128c
    Connecting to gcc.gnu.org (gcc.gnu.org)|8.43.85.97|:443... connected.
    ERROR: The certificate of ‘gcc.gnu.org’ is not trusted.
    ERROR: The certificate of ‘gcc.gnu.org’ is not yet activated.
    The certificate has not yet been activated
    
    

    解决方法·

    ## 绕过 SSL 验证
    wget --no-check-certificate https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
    

    4. 参考文档

  • 相关阅读:
    牛客网 ACM 模式 输入输出
    [vue] nvm
    Web服务(Web Service)
    【文末送书】计算机网络编程 | epoll详解
    Scala 入门指南:从零开始的大数据开发
    java计算机毕业设计积分权益商城MyBatis+系统+LW文档+源码+调试部署
    5. HBase必知必会之理论进阶篇
    Qt_C++读取RFID卡号支持Windows统信麒麟国产Linux系统
    充电宝后台被攻击怎么办
    MyBatis在循环内查询序列值重复解决方法
  • 原文地址:https://blog.csdn.net/qq_43475285/article/details/140409390