【香菇带你学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. 参考文档

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/798247.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

TensorBoard ,PIL 和 OpenCV 在深度学习中的应用

重要工具介绍 TensorBoard: 是一个TensorFlow提供的强大工具,用于可视化和理解深度学习模型的训练过程和结果。下面我将介绍TensorBoard的相关知识和使用方法。 TensorBoard 简介 TensorBoard是TensorFlow提供的一个可视化工具,用于&#x…

JVM:垃圾回收器

文章目录 一、介绍二、年轻代-Serial垃圾回收器三、老年代-SerialOld垃圾回收器四、年轻代-ParNew垃圾回收器五、老年代-CMS(Concurrent Mark Sweep)垃圾回收器六、年轻代-Parllel Scavenge垃圾回收器七、Parallel Old垃圾回收器八、G1垃圾回收器 一、介…

Python实战MySQL:数据库操作全流程详解

更多Python学习内容:ipengtao.com MySQL是一种广泛使用的关系型数据库管理系统,Python可以通过多种方式与MySQL进行交互。本文将详细介绍如何使用Python操作MySQL数据库,包括安装必要的库、连接数据库、执行基本的CRUD(创建、读取…

JAVA String类最全分析

一、介绍 StringSerializable实现它,String可以串行化,可以在网络上传输ComparableString对象可以相互比较CharSequenceObject String class Main{public static void main(String[] args) {//1.String 对象用于保存字符串,也就是一组字符…

Android ImageDecoder把瘦高/扁平大图相当于fitCenter模式decode成目标小尺寸Bitmap,Kotlin

Android ImageDecoder把瘦高/扁平大图相当于fitCenter模式decode成目标小尺寸Bitmap,Kotlin val sz Size(MainActivity.SIZE, MainActivity.SIZE)val src ImageDecoder.createSource(mContext?.contentResolver!!, uri)val bitmap ImageDecoder.decodeBitmap(sr…

MySQL更新和删除(DML)

DML-修改数据 UPDATE 表名 SET 字段1 值1,字段2值2,....[WHERE 条件] 例如 1.这个就是把employee表中的这个name字段里面并且id字段为1的名字改为itheima update employee set nameitheima where id 1; 2.这个就是把employee这个表中的name字段和…

SpringMVC源码分析

文章目录 概要启动阶段请求阶段 概要 以下是调试mvc源码过程中用到的demo以及配置文件 webapp/WEB-INF/web.xml <?xml version"1.0" encoding"UTF-8"?> <web-app xmlns"http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi"http://…

熊海CMS漏洞练习平台的一次xss、sql注入、越权黑盒思路分析

简介 熊海CMS是由熊海开发的一款功能丰富的网站综合管理系统&#xff0c;广泛应用于个人博客、个人网站以及企业网站&#xff0c;本文章用于黑盒测试&#xff0c;如果需要「源码审计」后台回复【「CMS」】获取即可&#xff0c;精心准备了40多个cms源码漏洞平台&#xff0c;供宝…

记一次docker容器安装MySQL,navicat无法连接报错(10060错误)

今天在云服务器上使用docker部署mysql 8.0.11时&#xff0c;遇到了一个诡异的问题&#xff0c;在云服务器的docker容器内可以连接上mysql&#xff0c;然而在自己电脑上连接mysql时报错&#xff1a;Can‘t connect to MySQL server on localhost (10060) 下面是网上搜寻的几种可…

《SpringBoot 整合 Prometheus 采集自定义指标》

&#x1f4e2; 大家好&#xff0c;我是 【战神刘玉栋】&#xff0c;有10多年的研发经验&#xff0c;致力于前后端技术栈的知识沉淀和传播。 &#x1f497; &#x1f33b; 近期刚转战 CSDN&#xff0c;会严格把控文章质量&#xff0c;绝不滥竽充数&#xff0c;如需交流&#xff…

C语言 ——— const关键字

目录 const修饰变量 const修饰指针变量 const放在指针类型之前 const放在指针类型之后 小结 const修饰变量 当 const 修饰 int类型 的 变量a 后&#xff0c;此时的 变量a 就具有长属性&#xff0c;就不能被赋值为其他的值 将 变量a的地址 存储到 指针变量pa 中&#xff…

计算机网络——常见问题汇总

1. introduction 1.1 Explain what a communication protocol is and why its important. A communication protocol is a set of rules and conventions(公约) that govern(统治) how data is transmitted and received between devices(设备), systems, or entities in a ne…

1、BOREDHACKERBLOG:社交网络

靶机&#xff1a;https://www.vulnhub.com/entry/boredhackerblog-social-network,454/ 参考&#xff1a;Vulnhub靶机&#xff1a;BOREDHACKERBLOG: SOCIAL NETWORK_boredhackerblog系列-CSDN博客 需要使用virtualbox。 先去官网下载了最新版的vietualbox&#xff0c;以及把这…

使用 Unstructured.io 和 Elasticsearch 向量数据库搜索复杂文档

作者&#xff1a;来自 Elastic Amy Ghate, Rishikesh Radhakrishnan, Hemant Malik 使用非结构化和 Elasticsearch 向量数据库为 RAG 应用程序提取和搜索复杂的专有文档 在使信息可搜索之前解析文档是构建实际 RAG 应用程序的重要步骤。Unstructured.io 和 Elasticsearch 在此…

Admin.NET源码学习(2:安装并运行前端)

根据Admin.NET的GitHub主页介绍&#xff0c;前端运行步骤需要运行pnpm命令。百度pnpm的话&#xff0c;需要支持npm相关的命令支持。   根据参考文献4&#xff0c;安装Node.js后会提供npm命令支持&#xff08;npm是Node.js的软件包管理器&#xff0c;用于安装、发布和共享Jav…

FreeRTOS 入门 知识

什么是FreeRTOS FreeRTOS 是一个轻量级的实时操作系统&#xff08;RTOS&#xff09;&#xff0c;由 Richard Barry 在 2003 年开发&#xff0c;并且由亚马逊的 FreeRTOS 项目&#xff08;一个由 Amazon Web Services (AWS) 支持的开源项目&#xff09;进一步推动和发展。FreeR…

顺序表算法 - 合并两个有序数组

88. 合并两个有序数组 - 力扣&#xff08;LeetCode&#xff09;https://leetcode.cn/problems/merge-sorted-array/description/思路: void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) {int l1,l2,l3;l1 m-1;l2 n-1;l3 mn-1;//l1和l2大于…

DispatcherServlet懒加载带来的问题和思考

问题 DispatcherServlet的懒加载会导致在用户在进行第一次请求的时候会比正常慢很多&#xff0c;如果这个时候大量请求同时过来&#xff0c;那么阻塞和cpu的暴增就会显而易见。 背景 在回顾SpringMvc对servlet的增强的过程中&#xff0c;突然发现DispatcherServlet是懒加载的…

7.2 AQS原理

AQS 原理 概述 全称是 AbstractQueuedSynchronizer&#xff0c;是阻塞式锁和相关的同步器工具的框架。 特点&#xff1a; 用 state 属性来表示资源的状态&#xff08;分独占模式和共享模式&#xff09;&#xff0c;子类需要定义如何维护这个状态&#xff0c;控制如何获取锁和…

JAVA自定义注释

interface 声明 package test; public interface InProgress { } InProgress public void calculateInterest(float amount, float rate) { } 带成员 public interface TODO {String value(); } InProgress //只有成员变量名有value时&#xff0c;值有给value赋值时可以这…