当前位置:网站首页>GCC [6] - 4 stages of compilation
GCC [6] - 4 stages of compilation
2022-07-04 14:16:00 【Small snails in big cities】
List of articles
Preface
GCC Compilation is divided into 4 Stages : Preprocessing 、 compile 、 assembly 、 link
gcc General format of instruction
gcc 【 Options 】 Files to compile 【 Options 】【 The output file 】
| Options | explain |
|---|---|
| -E | control GCC The compiler only preprocesses the source code |
| -S | control GCC The compiler only deals with the compilation phase of the specified file |
| -c | control GCC The compiler only deals with the formulated files to the assembly stage , And generate the corresponding target file |
| -o outfile | Specifies the filename of the output file |
eg: Here is a very simple paragraph hello.c Code , Take this code for example
#include <stdio.h>
#define NAME "mao"
void main()
{
printf("hello:%s\n",NAME);
}
One 、 Pretreatment stage
Preprocessing The main work of this stage is to delete all comments in the program 、 Deal with # The first order . Such as : Expansion of header file 、 Replacement of macro definitions .
For the above code #include <stdio.h> Indicates the import header file , In the pretreatment stage stdio.h The file will be loaded into my hello.c in , And for the... Used in the code NAME, It will be replaced with the real content defined by the macro mao, You can use the following command to perform preprocessing : Generate .i file ,linux The suffix of the file after preprocessing in the system is usually .i
gcc -E hello.c -o hello.i
Two 、 Compile phase
gcc In the compilation stage, various checks will be carried out on the preprocessed code ( Lexical analysis , Syntax analysis , Semantic analysis ), To determine whether our code conforms to the specification , If you generate assembly code (.s Final document )
Be careful :gcc -S You can't only compile pre processed .i file , It means control GCC The compiler only processes the specified file to the compilation stage
- If the file to be compiled is preprocessed
.ifile , beGCCThe compiler only needs to compile this file . - If the file to be compiled is
.cperhaps.cppSource file , beGCCThe compiler preprocesses and compiles it ( Two steps ).
gcc -S hello.i -o hello.s
3、 ... and 、 Assembly stage
hold .s The file is translated into binary .o file ( Target file )
Be careful :gcc -c It's not just about editing .s file , Its meaning is to control GCC The compiler only processes the specified file to the assembly stage
- If the specified file is
.cfile , begcc -cThe instruction will execute on the file Preprocessing 、 compile 、 assembly . - If the specified file is preprocessed
.ifile , begcc -cInstruction to compile and assemble the file . - If the specified file is a compiled file , be
gcc -cInstruction only performs assembly operations on this file .
gcc -c hello.s -o hello.o
Four 、 Link phase
The assembly phase compiles the code into binary files , And other components of the system ( For example, standard library 、 Dynamic link library, etc ) Combined to work properly , For example, call print Function print , In the preprocessing stage, it is only “stdio.h” The declaration in the header file is introduced , There is no function implementation , Then how to call it ?
This is the job of linking , Before linking, we deal with files , And linking is the process of packaging multiple files , It combines all object files and system components into an executable .
gcc hello.o -o hello
边栏推荐
- 【C 题集】of Ⅶ
- QT how to detect whether the mouse is on a control
- 读取 Excel 表数据
- [antd step pit] antd form cooperates with input Form The height occupied by item is incorrect
- Worried about "cutting off gas", Germany is revising the energy security law
- Ws2818m is packaged in cpc8. It is a special circuit for three channel LED drive control. External IC full-color double signal 5v32 lamp programmable LED lamp with outdoor engineering
- IP 实验室月复盘 · 第 5 期
- Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
- [R language data science]: cross validation and looking back
- Blob, text geometry or JSON column'xxx'can't have a default value query question
猜你喜欢

学内核之三:使用GDB跟踪内核调用链

瑞吉外卖笔记

基于YOLOv1的口罩佩戴检测

Xcode 异常图片导致ipa包增大问题

Why should Base64 encoding be used for image transmission

Install MySQL

Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters

1200. 最小绝对差

为什么图片传输要使用base64编码
![[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions](/img/43/1a9786c89a5ab21d1fb8903cb7b77e.png)
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
随机推荐
瑞吉外卖笔记
吃透Chisel语言.05.Chisel基础(二)——组合电路与运算符
富文本编辑:wangEditor使用教程
测试流程整理(2)
Huahao Zhongtian rushes to the scientific and Technological Innovation Board: the annual loss is 280million, and it is proposed to raise 1.5 billion. Beida pharmaceutical is a shareholder
Install MySQL
按照功能对Boost库进行分类
2022 practice questions and mock exams for the main principals of hazardous chemical business units
Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
mac redis安装与使用,连接远程服务器 redis
学内核之三:使用GDB跟踪内核调用链
PHP log debugging
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
php 日志调试
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
Read excel table data
JVM 内存布局详解,图文并茂,写得太好了!
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect