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

打印 上一主题 下一主题

主题 1783|帖子 1783|积分 5351

0. 前言

gcc(GNU Compiler Collection)是GNU项目标一部分,它是一个支持多种编程语言的编译器聚集,但最常用的是作为C和C++的编译器。GCC能够编译、汇编和链接C、C++、Objective-C、Fortran、Ada、Go以及D等多种语言的程序。它因其跨平台性、高效性和机动性而受到广泛的欢迎和使用。
我的系统类型规格如下,openeuler属于redhat/centos系列。Ubuntu系列主机本文仅供参考。
  1. [root@localhost ~]# cat /etc/redhat-release
  2. BigCloud Enterprise Linux For Euler release 21.10 (LTS-SP2)
  3. [root@localhost ~]# cat /etc/os-release
  4. NAME="BigCloud Enterprise Linux"
  5. VERSION="21.10 (LTS-SP2)"
  6. ID="bclinux"
  7. VERSION_ID="21.10"
  8. PRETTY_NAME="BigCloud Enterprise Linux For Euler 21.10 LTS"
  9. ANSI_COLOR="0;31"
  10. [root@localhost ~]# free -g
  11.               total        used        free      shared  buff/cache   available
  12. Mem:             15           0          14           0           0          14
  13. Swap:             7           0           7
复制代码
近来要在BClinux for openeuler上安装mysql8.0结果各种报错,缺少很多依赖。惋惜系统自带的yum源要么就是没有这个安装包,要么就是软件版本不符合要求。所以只能选择源码编译安装。当前系统gcc版本为7.3.0,要升级到10以上.
安装gcc前须要安装GMP、MPFR、MPC这三个依赖库
  1. # 查看当前系统gcc版本
  2. root@localhost ~]# gcc --version
  3. gcc (GCC) 7.3.0
  4. Copyright © 2017 Free Software Foundation, Inc.
  5. 本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
  6. 包括没有适销性和某一专用目的下的适用性担保。
复制代码
1. 安装前预备工作

   假如须要再root下直接安装,请参考文章最后的root下编译安装gcc脚本
  1.1 创建weihu用户

   养成良好安装习惯,不使用root直接安装。假如须要再root下安装,请略过本文1.1小节即可
  这里我们注册一个weihu用户,并赋予维护用户sudo权限。然后使用weihu用户安装
  1. # 创建weihu用户
  2. [root@localhost ~]# useradd -m weihu
  3. # 设置weihu的密码
  4. [root@localhost ~]# passwd weihu
  5. 更改用户 weihu 的密码 。
  6. 新的 密码:
  7. 重新输入新的 密码:
  8. passwd:所有的身份验证令牌已经成功更新。
复制代码
赋予weihu用户sudo权限
  1. # 编辑 /etc/sudoers文件 ,找到下面的一行内容
  2. # root    ALL=(ALL)       ALL
  3. [root@localhost ~]# vi /etc/sudoers
复制代码

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

保存退出
   接下来的所有操作均在weihu用户下面操作
  1. # 登录weihu用户
  2. [root@localhost ~]# su - weihu
复制代码
1.2 安装依赖包

编译安装gcc之前,须要安装GMP、MPFR、MPC三个依赖。且三个依赖包的安装次序由先后。同样,我们也须要分别编译安装(使用yum安装的版本较低,报错较多)
安装之前,先建个文件夹用于存放源码
  1. # 将
  2. [weihu@localhost ~]$ mkdir /home/weihu/soft
  3. [weihu@localhost ~]$ cd /home/weihu/soft
复制代码
1.2.1 安装 GMP

CMP下载网址:https://gcc.gnu.org/pub/gcc/infrastructure/
这里我们下周最新的版本6.2.1版本

可以下载到本地再上传到Linux主机,若Linux主机可以访问公网,也可以通过wget方向直接下载到Linux主机。
这里我选择第二种方法。
  1. # 下载gmp-6.2.1.tar.bz2源码
  2. [weihu@localhost ~]$ cd /home/weihu/soft/
  3. [weihu@localhost soft]$ wget https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
  4. # 解压gmp-6.2.1.tar.bz2源码
  5. [weihu@localhost soft]$ tar -xvf gmp-6.2.1.tar.bz2
  6. # 进入解压后的文件夹
  7. [weihu@localhost soft]$ cd gmp-6.2.1/
  8. # #创建并进入安装目录
  9. [weihu@localhost gmp-6.2.1]$ mkdir build
  10. [weihu@localhost gmp-6.2.1]$ cd build
  11. [weihu@localhost build]$
  12. # #配置安装
  13. [weihu@localhost build]$ ../configure -prefix=/usr/local/gmp-6.2.1
  14. # 编译
  15. weihu@localhost build]$ make -j$(nproc)
  16. # 安装
  17. [weihu@localhost build]$ sudo make install
复制代码
如许就把gmp安装在/usr/local/gmp-6.2.1路径下
   在进行编译安装的时间。我用的命令为 make -j$(nproc)
  使用 -j 选项可以指定同时运行的作业(即编译使命)的最大数量。假如 -j 背面跟的是一个数字,那么 make 会尝试同时运行指定命量的作业。假如不跟数字,大概跟的是 0,make 会尝试同时运行尽可能多的作业。
  1.2.2 安装MPFR

MPFR下载网址:https://gcc.gnu.org/pub/gcc/infrastructure/
本次,我们选择MPFR版本为4.1.0
  1. # 下载源码
  2. [weihu@localhost ~]$ cd /home/weihu/soft/
  3. [weihu@localhost soft]$ wget https://gcc.gnu.org/pub/gcc/infrastructure/mpfr-4.1.0.tar.bz2
  4. # 解压mpfr-4.1.0.tar.bz2
  5. [weihu@localhost soft]$ tar -xvf mpfr-4.1.0.tar.bz2
  6. # 进入解压后的文件
  7. [weihu@localhost soft]$ cd mpfr-4.1.0/
  8. # 新建构建文件夹并进入
  9. weihu@localhost mpfr-4.1.0]$ mkdir build
  10. weihu@localhost mpfr-4.1.0]$  cd build
  11. # 配置安装
  12. [weihu@localhost build]$ ../configure --prefix=/usr/local/mpfr-4.1.0 --with-gmp=/usr/local/gmp-6.2.1
  13. # 编译
  14. [weihu@localhost build]$ make -j$(nproc)
  15. # 安装
  16. [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
  1. # 下载源码
  2. [weihu@localhost soft]$ cd /home/weihu/soft
  3. [weihu@localhost soft]$ wget https://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.2.1.tar.gz
  4. #解压源码
  5. [weihu@localhost soft]$ tar -xvf mpc-1.2.1.tar.gz
  6. # 进入解压后的文件夹
  7. [weihu@localhost soft]$ cd mpc-1.2.1/
  8. # 创建构建文件夹并进入
  9. [weihu@localhost mpc-1.2.1]$ mkdir build
  10. [weihu@localhost mpc-1.2.1]$ cd build
  11. #配置安装
  12. [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
  13. # 编译
  14. [weihu@localhost build]$ make -j$(nproc)
  15. # 安装
  16. [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安装
  1. # 下载源码
  2. [weihu@localhost soft]$ cd /home/weihu/soft
  3. [weihu@localhost soft]$ wget https://gcc.gnu.org/pub/gcc/releases/gcc-10.1.0/gcc-10.1.0.tar.gz
  4. # 解压源码并进入
  5. [weihu@localhost soft]$ tar -xvzf gcc-10.1.0.tar.gz
  6. [weihu@localhost soft]$ cd gcc-10.1.0/
  7. # 创建构建文件夹并进入
  8. [weihu@localhost gcc-10.1.0]$ mkdir build
  9. [weihu@localhost gcc-10.1.0]$ cd build
  10. # 配置安装
  11. [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
  12. # 编译(时间较长)
  13. [weihu@localhost build]$ make -j$(nproc)
  14. # 安装
  15. [weihu@localhost build]$ sudo make install
复制代码
gcc至此安装成功,然后我们将gcc添加进入系统环境变量
  1. # 软链接
  2. [weihu@localhost ~]$ sudo ln -s /usr/local/gcc-10.1.0/bin/gcc gcc
  3. [weihu@localhost ~]$ sudo ln -s /usr/local/gcc-10.1.0/bin/g++ g++
  4. [weihu@localhost ~]$ export PATH=/usr/local/gcc-10.1.0/bin:$PATH
复制代码
查看gcc版本
  1. [weihu@localhost ~]$ gcc --version
  2. gcc (GCC) 10.1.0
  3. Copyright (C) 2020 Free Software Foundation, Inc.
  4. This is free software; see the source for copying conditions.  There is NO
  5. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  6. [weihu@localhost ~]$
复制代码


3. 报错办理

3. 1. wget下载报错

  1. [weihu@localhost soft]$ wget https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
  2. --2024-03-28 00:17:13--  https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
  3. Resolving gcc.gnu.org (gcc.gnu.org)... 8.43.85.97, 2620:52:3:1:0:246e:9693:128c
  4. Connecting to gcc.gnu.org (gcc.gnu.org)|8.43.85.97|:443... connected.
  5. ERROR: The certificate of ‘gcc.gnu.org’ is not trusted.
  6. ERROR: The certificate of ‘gcc.gnu.org’ is not yet activated.
  7. The certificate has not yet been activated
复制代码
办理方法·
  1. ## 绕过 SSL 验证
  2. wget --no-check-certificate https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
复制代码
4. 参考文档



  • https://cloud.tencent.com/developer/article/2112576

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

祗疼妳一个

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表