当前位置:网站首页>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
.i
file , beGCC
The compiler only needs to compile this file . - If the file to be compiled is
.c
perhaps.cpp
Source file , beGCC
The 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
.c
file , begcc -c
The instruction will execute on the file Preprocessing 、 compile 、 assembly . - If the specified file is preprocessed
.i
file , begcc -c
Instruction to compile and assemble the file . - If the specified file is a compiled file , be
gcc -c
Instruction 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
边栏推荐
- 【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
- 锐成芯微冲刺科创板:年营收3.67亿拟募资13亿 大唐电信是股东
- Understand chisel language thoroughly 04. Chisel Foundation (I) - signal type and constant
- The font of markdown grammar is marked in red
- Unity Shader学习(三)试着绘制一个圆
- Variable promotion and function promotion in JS
- Install and use MAC redis, connect to remote server redis
- go vendor 项目迁移到 mod 项目
- 小程序直播 + 电商,想做新零售电商就用它吧!
- The Secretary of Homeland Security warned immigrants "not to embark on a dangerous journey"
猜你喜欢
英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value
[antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp
测试流程整理(2)
1200. 最小绝对差
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
How to package QT and share exe
Unittest框架中引入TestFixture
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
随机推荐
golang fmt. Printf() (turn)
FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
吃透Chisel语言.07.Chisel基础(四)——Bundle和Vec
Unity shader learning (3) try to draw a circle
数据仓库面试问题准备
Fs7867s is a voltage detection chip used for power supply voltage monitoring of digital system
Unity Shader学习(三)试着绘制一个圆
C# wpf 实现截屏框实时截屏功能
吃透Chisel语言.04.Chisel基础(一)——信号类型和常量
基于PaddleX的智能零售柜商品识别
【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
sharding key type not supported
海外游戏代投需要注意的
做事的真正意义和目的,真正想得到什么
Worried about "cutting off gas", Germany is revising the energy security law
MySQL之详解索引
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
富文本编辑:wangEditor使用教程
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
Can mortgage with housing exclude compulsory execution