GUN compiler collection源代码编译过程

2024-06-03 23:04

本文主要是介绍GUN compiler collection源代码编译过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第一部分:

学习kernel需要了解编译的一些过程,为了详细理解GCC编译过程的原理,动手做了个sy,记录如下,有需要的童鞋可以参考。

1.环境:(均可,二次环境并非是WSL版本)

root@LAPTOP-X280:~# uname -r

5.15.146.1-microsoft-standard-WSL2

root@LAPTOP-X280:~# lsb_release -a

No LSB modules are available.

Distributor ID: Ubuntu

Description:    Ubuntu 22.04.3 LTS

Release:        22.04

Codename:       jammy

root@LAPTOP-X280:~# gcc -v

Command 'gcc' not found, but can be installed with:

apt install gcc

root@LAPTOP-X280:~#

2.Downloading the source 

从亚洲镜像站点下载:https://www.gnu.org/prep/ftp.html#asia

下载最新的版本:

NJU Mirror

The installation procedure itself is broken into five steps.

  1. Prerequisites 
  2. Downloading the source 
  3. Configuration 
  4. Building 
  5. Testing (optional)
  6. Final install 

来自 <Installing GCC- GNU Project>

0.解压缩文件

xz -cd gcc.13.0.tar.xz | tar xvf -

 mkdir objdir

1.Prerequisites  

apt install build-essential   //必须安装的基础编译,不如C和C++

//解决configure: error: no acceptable C compiler found in $PATH等问题

//实际上这个是安装了gcc,g++,make等工具的编译器了。

apt install libgmp-dev libmpfr-dev libmpc-dev libisl-dev //必须安装,按这里的先后顺序

3.Configuration 

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir# ../configure

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir# ../configure --disable-multilib

4.Building  

Building a native compiler

nproc

make -j$(nproc)

这个过程需要时间,而且top查看,开销很大,CPU基本都是耗尽。

若出现cpu等fetal error,可能就不需要使用-j的选项了,默认使用make即可

以下是WSL的资源使用:

Win11 的资源使用:

卡住在:…/gcc /explow.cc

后面出现fatal error

Ctrl+c终止进程,改为make -j6  记得6<nproc(8),资源也是很吃紧如下图:

还是卡住了:

/bin/bash ../../gcc/../move-if-change tmp-fixinc_list fixinc_list

echo timestamp > s-fixinc_list

….

最后尝试make把

先make distclean,

do ‘make distclean’ to delete all files that might be invalid.

再../configure --disable-multilib

Make  //19:10开始执行命令,完成时间:20:03终止

这时候的资源:

执行make -j8 开始执行时间:20:04,完成时间:21:10,出现fatal error

这时候的资源情况瞬间上升:

第二次改成make -j6,时间从21:21-21:46

这次OK了。

ps:(这里是第二次编译的,遇到磁盘空间不够的情况)

 磁盘空间不够的化使用命令:make -j4 bootstrap-lean

5.Testing (optional)   

The latest development sources are available via anonymous Git. Use the following command to clone a copy of the source tree:

     $ git clone git://git.sv.gnu.org/dejagnu.git

 cd dejagnu

./configure

 make

…出错解决:

apt install texinfo

make

 make install

cd objdir;

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir# make -k check

6.Final install 

 make install

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir# whereis gcc

gcc: /usr/lib/gcc /usr/local/bin/gcc /usr/local/lib/gcc /usr/share/gcc

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir# /usr/local/bin/g

cc -v

Using built-in specs.

COLLECT_GCC=/usr/local/bin/gcc

COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/13.3.0/lto-wrapper

Target: x86_64-pc-linux-gnu

Configured with: ../configure --disable-multilib

Thread model: posix

Supported LTO compression algorithms: zlib

gcc version 13.3.0 (GCC)

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir#

第二部分:

使用gcc编译c程序

1.先设置环境变量

查看:

因为在root环境下编译的源代码,所以切换root下查看:

这里就可以直接使用gcc -v了,所以不需要设置环境变量。

关于环境变量的设置,查看/etc/profile或需要用到:

export HOME等命令, 如export PATH= 'echo $PATH':/root/linux

2.编译单个文件

ps,了解以下:

但是并不是说gcc编译c语言,g++编译c++。而是:gcc调用了C compiler,而g++调用了C++ compiler。主要区别在于:

  •     对于 .c和.cpp文件,gcc分别当做c和cpp文件编译(c和cpp的语法强度是不一样的)
  •     对于 .c和.cpp文件,g++则统一当做cpp文件编译
  •     使用g++编译文件时,g++会自动链接标准库STL,而gcc不会自动链接STL
  •     gcc在编译C文件时,可使用的预定义宏是比较少的
  •     gcc在编译cpp文件时/g++在编译c文件和cpp文件时(这时候gcc和g++调用的都是cpp文件的编译器),会加入一些额外的宏。
  •     在用gcc编译c++文件时,为了能够使用STL,需要加参数 –lstdc++ ,但这并不代表 gcc –lstdc++ 和 g++等价,它们的区别不仅仅是这个。
  • gcc是GCC中的GUNC Compiler(C 编译器)
  • g++是GCC中的GUN C++ Compiler(C++编译器)

linux@LAPTOP-X280:~/cfile$ cat hello.c

#include <stdio.h>

int main(void)

{

        printf("hello,\nworld!\n");

        return 0;

}

缺省一步编译

linux@LAPTOP-X280:~/cfile$ gcc hello.c   //直接执行编译,会生产a.out的文件

linux@LAPTOP-X280:~/cfile$ ls

a.out  hello.c  hi.c

linux@LAPTOP-X280:~/cfile$ ./a.out    //执行a.out 二进制可执行文件

hello,

world!

linux@LAPTOP-X280:~/cfile$ file a.out

a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, not stripped

指定一步编译:

inux@LAPTOP-X280:~/cfile$ gcc hello.c -o myhello

linux@LAPTOP-X280:~/cfile$ file myhello

myhello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, not stripped

linux@LAPTOP-X280:~/cfile$ ./myhello

hello,

world!

分步编译:

预处理:gcc -E hello.c -o hello.i  //-E(大写)只进行预处理

编  译: gcc -S hello.i -o hello.s   //-S(大写)只进行预处理和编译

汇  编: gcc -c hello.s -o hello.o  //-c 只进行预处理、编译和汇编

链  接: gcc    hello.o -o file   //指定生成的输出文件名为file

linux@LAPTOP-X280:~/cfile$ gcc -E hello.c -o h.i

linux@LAPTOP-X280:~/cfile$ cat h.i |wc -l

745

linux@LAPTOP-X280:~/cfile$ cat h.i

linux@LAPTOP-X280:~/cfile$ gcc -S h.i -o h.s

linux@LAPTOP-X280:~/cfile$ cat h.o |wc -l

27

linux@LAPTOP-X280:~/cfile$ cat h.o

linux@LAPTOP-X280:~/cfile$ file h

h: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, not stripped

linux@LAPTOP-X280:~/cfile$ ./h

hello,

world!

使用readelf -a h.o查看.o文件

3.编译多个文件

4.自动编译文件

这篇关于GUN compiler collection源代码编译过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1028361

相关文章

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

Spring Security注解方式权限控制过程

《SpringSecurity注解方式权限控制过程》:本文主要介绍SpringSecurity注解方式权限控制过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、摘要二、实现步骤2.1 在配置类中添加权限注解的支持2.2 创建Controller类2.3 Us

Spring AI集成DeepSeek三步搞定Java智能应用的详细过程

《SpringAI集成DeepSeek三步搞定Java智能应用的详细过程》本文介绍了如何使用SpringAI集成DeepSeek,一个国内顶尖的多模态大模型,SpringAI提供了一套统一的接口,简... 目录DeepSeek 介绍Spring AI 是什么?Spring AI 的主要功能包括1、环境准备2

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

pycharm远程连接服务器运行pytorch的过程详解

《pycharm远程连接服务器运行pytorch的过程详解》:本文主要介绍在Linux环境下使用Anaconda管理不同版本的Python环境,并通过PyCharm远程连接服务器来运行PyTorc... 目录linux部署pytorch背景介绍Anaconda安装Linux安装pytorch虚拟环境安装cu

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Java中对象的创建和销毁过程详析

《Java中对象的创建和销毁过程详析》:本文主要介绍Java中对象的创建和销毁过程,对象的创建过程包括类加载检查、内存分配、初始化零值内存、设置对象头和执行init方法,对象的销毁过程由垃圾回收机... 目录前言对象的创建过程1. 类加载检查2China编程. 分配内存3. 初始化零值4. 设置对象头5. 执行

SpringBoot整合easy-es的详细过程

《SpringBoot整合easy-es的详细过程》本文介绍了EasyES,一个基于Elasticsearch的ORM框架,旨在简化开发流程并提高效率,EasyES支持SpringBoot框架,并提供... 目录一、easy-es简介二、实现基于Spring Boot框架的应用程序代码1.添加相关依赖2.添

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入