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

相关文章

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL中的InnoDB单表访问过程

《MySQL中的InnoDB单表访问过程》:本文主要介绍MySQL中的InnoDB单表访问过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、访问类型【1】const【2】ref【3】ref_or_null【4】range【5】index【6】

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机