当前位置:网站首页>Program translation and execution, from editing, preprocessing, compilation, assembly, linking to execution
Program translation and execution, from editing, preprocessing, compilation, assembly, linking to execution
2022-07-27 10:18:00 【Ahao_ te】
List of articles
1、 Program translation environment and execution environment
stay C In any implementation of language , There are two environments .
- Translation environment , In this environment, the source code is converted into executable machine instructions .
- Running environment , Actually execute the code .
2、 Compile and link
2.1、 A hidden process
Here is a program we are familiar with .
#include<stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
We can easily print successfully on our screen by compiling and running in the compiler ,
But in fact , In fact, the compilation stage also includes Preprocessing 、 compile 、 assembly 、 link .
Pictured :

In general
- The source file of the program passes compile Process is converted into object code , Generate target file (.obj(win In the environment ) or .o(linux Environmental Science )).
- If there are multiple source files , The linker in the link will bind them together , Finally, an executable file is formed .
- Links combine multiple source files and link libraries , Finally, an executable file is generated . Link libraries provide us with other library functions that need to be introduced .

2.2、 Details of the compilation process
2.2.1、 Preprocessing
In the preprocessing process, we mainly deal with those in the source code file with # Precompiled instructions at the beginning , Such as #include、#define
The following rules .
- Will all " #define " Delete , And expand all the macro definitions ( All replacement ).
- Process conditional precompile instructions , Such as #if、#ifdef、#elif、#else、#endif.
- Handle " #include " Precompiling instructions , Insert the included file into the location of the precompiled instruction . Note that this process is recursive , That is, one file may contain another file .
- Delete all comments .
- Keep all #pragma Compiler Directives , Because compilers need to use them .
After pretreatment .i The file no longer contains any macro definitions .
2.2.2、 compile
During compilation , Will experience 4 A process , Lexical analysis 、 Syntax analysis 、 Semantic analysis 、 Symbol summary , Convert the source code to Assembly code Then store it in .s In file .
In every process :
- Lexical analysis : Through a call lex The program divides the character sequence of the source code into a series of marks , This token has an identifier 、 keyword 、 Literal ( Like numbers 、 character string ).
- Syntax analysis : Parsing tokens through a parser , Form a grammar tree , Complete the syntax analysis of some expressions .
- Semantic analysis : Analyze whether the grammar is meaningful through semantic analyzer ( For example, pointer multiplication is meaningless , But it is legal in grammatical analysis , So semantic analysis is about meaning ).
- Symbol summary : It will summarize the global variables and functions in the source code , Form a table , Easy to use in subsequent links .
For example, yes. Test.c Source file , take Test.c Inside Add and main Summary .
Yes add.c, take add.c Inside Add Summary .
2.2.3、 assembly
Compilation will Assembly code convert to Binary instructions , Generate target file (.obj(win In the environment ) or .o(linux Environmental Science )).
It is worth noting that this process will form a symbol table , During the previous compilation , We summarize global variables and functions , Here we add an address to the members in the summary , It forms a symbol table .
The symbol table will be linked in the following .

2.2.4、 link
There are two actions in the linking process , One is the consolidated segment table , Another is the merging and relocation of symbol tables .
1. Merge segment tables
Formed after compilation .o file , The data in it is linux The environment is made up of elf Stored in this format , such elf Format will divide the file into many segments , Each segment stores different data .
If more than one .o file ( Target file ), As mentioned earlier, it will link with the link library to form an executable file (.exe), The executable file is also elf This format , So the previous ones .o In the document was elf The same segments divided by format need to be merged .

2. Merging and relocating symbol tables
In this operation , Merged the address of the symbol table in the assembly , And relocating the addresses of individual members .
such as :
This will ensure that the program runs , The function we use is valid , But if it doesn't exist Add function , Then the merged address will be invalid , Linked errors will occur after the program is executed .
such as :
3、 perform
The process of program execution :
- The program must be loaded into memory . In an operating system environment : This is usually done by the operating system . In an independent environment , Program
The loading of must be manually scheduled , It can also be done by putting executable code into read-only memory . - The execution of the procedure begins . And then I call main function .
- Start executing program code . At this point, the program will use a runtime stack (stack)( Here you can refer to Function stack frame creation and destruction ), Store function local variables and return address . Programs can also use static (static) Memory , Variables stored in static memory retain their values throughout the execution of the program .
- To terminate the program . Normal termination main function ; It could be an accidental termination .
End of this chapter .
边栏推荐
猜你喜欢

数据库操作基础语句

Switch port mirroring Configuration Guide

Concurrent thread state transition

Food safety | the kitchen board environment is very important. Do you know these use details?

VS2019+CUDA11.1新建项目里没有CUDA选项

关于ETL的两种架构(ETL架构和ELT架构)

Metasploit-永恒之蓝攻击

Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0

ACL2021最佳论文出炉,来自字节跳动

Dcgan paper improvements + simplified code
随机推荐
Anchor free detector: centernet
3D restoration paper: shape painting using 3D generative advantageous networks and recurrent revolutionary networks
open3d库的安装,conda常用指令,导入open3d时报这个错误Solving environment: failed with initial frozen solve. Retrying w
matlab-绘制分叉与混沌分支图
Matlab-绘制日期和持续时间图
Visual slam lecture notes (I): Lecture 1 + Lecture 2
文件上传漏洞相关
Mysql database experiment training 5, data query YGGL database query (detailed)
Practice and exploration of overseas site Seata of ant group
Ant advanced -path and fileset
QT learning (II) -.Pro file explanation
Failure of CUDA installation nsight visual studio edition failed
SE(Squeeze and Excitation)模块的理解以及代码实现
女粉想要找男朋友,竟是为了...
FSM onehot answer record
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
NFS 服务器的搭建
hdu5288(OO’s Sequence)
Ant高级-path和fileset
Hugo learning notes