当前位置:首页 » 《关于电脑》 » 正文

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

20 人参与  2024年10月13日 13:20  分类 : 《关于电脑》  评论

点击全文阅读


文章目录
0. 前言1. 安装前准备工作 1.1 创建weihu用户1.2 安装依赖包 1.2.1 安装 GMP1.2.2 安装MPFR1.2.3 安装MPC 2. gcc10.0.1版本安装3. 报错解决 3. 1. wget下载报错 4. 参考文档

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   availableMem:             15           0          14           0           0          14Swap:             7           0           7
1.2.3.4.5.6.7.8.9.10.11.12.13.14.

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

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

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

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:所有的身份验证令牌已经成功更新。
1.2.3.4.5.6.7.8.

赋予weihu用户sudo权限

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

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

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

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

保存退出

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

登录后复制
# 登录weihu用户[root@localhost ~]# su - weihu
1.2.
1.2 安装依赖包

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

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

登录后复制
# 将[weihu@localhost ~]$ mkdir /home/weihu/soft[weihu@localhost ~]$ cd /home/weihu/soft
1.2.3.
1.2.1 安装 GMP

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

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

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

可以下载到本地再上传到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
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.

这样就把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
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.

这样就把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
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.

这样就把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
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.

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
1.2.3.4.

查看gcc版本

登录后复制
[weihu@localhost ~]$ gcc --versiongcc (GCC) 10.1.0Copyright (C) 2020 Free Software Foundation, Inc.This is free software; see the source for copying conditions.  There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.[weihu@localhost ~]$
1.2.3.4.5.6.7.

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.bz2Resolving gcc.gnu.org (gcc.gnu.org)... 8.43.85.97, 2620:52:3:1:0:246e:9693:128cConnecting 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
1.2.3.4.5.6.7.

解决方法·

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

4. 参考文档

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

点击全文阅读


本文链接:http://m.zhangshiyu.com/post/171314.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • (此去经年无故人)南初陆南城:结局+番外精品选集起点章节+阅读即将发布预订
  • 沈凝夏叶晚怡附加完整在线阅读(归雁不栖故人枝)最近更新列表
  • 剧情人物是时初,白浩雄的玄幻言情小说《召诸神,踏万界,天命帝女逆乾坤》,由网络作家&ldquo;海鸥&rdquo;所著,情节扣人心弦,本站TXT全本,欢迎阅读!本书共计381345字,185章节,:结局+番外免费品鉴:结局+番外评价五颗星
  • 凤青禾,江明远,***枢小说(别人修仙我捡漏,卷王们破防了)最近更新(凤青禾,江明远,***枢)整本无套路阅读
  • 薛梨小说无删减+后续(曾经亲情似草芥)畅享阅读
  • 沈南栀小说(穿越时空,我要修补时空裂缝)章节目录+起点章节(沈南栀)全篇清爽版在线
  • 未婚妻被巨蟒缠身,我该吃就吃该喝就喝前言+后续_阿豪林月周然后续+番外_小说后续在线阅读_无删减免费完结_
  • 陆骁,陆本初小说(陆骁,陆本初)(癫!睁眼穿成老太太挥鞭***逆子)前传+阅读全新作品预订
  • 姐姐含冤而死后冥王另娶,我杀穿整个地府在线阅读_阎罗殿殷红别提一口气完结_小说后续在线阅读_无删减免费完结_
  • (书荒必看)毒后重生:疯王的神医小娇妻沈清歌,萧绝:+后续热血十足
  • 重生后我和太监联手灭了敌国喻辰,林雪续集(重生后我和太监联手灭了敌国)终极反转(喻辰,林雪)全篇一口气阅读
  • 我不做灵媒后,自称灵媒摆渡人的养妹害怕了内容精选_苏晓霍老阿姐无广告_小说后续在线阅读_无删减免费完结_

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1