当前位置:网站首页>Details such as compiling pretreatment
Details such as compiling pretreatment
2022-08-05 02:48:00 【Generally passing by half-margin】
Anyone who has written code knows it,Any program needs to be compiled and linked to produce an executable program,to make the code work,Then the process of compiling and linking,What does the compiler do,Make the program executable?
程序的翻译环境和执行环境
国际c标准中规定,any kind of implementation,都存在两个不同的环境.
1:翻译环境:将代码翻译成机器指令;
2:执行环境:used to actually execute the code;
What we need to understand is what the translation environment actually does,An executable program is generated.
翻译环境
图1.编译文件
如图1.The compiled file is shown,Each source file will be generated by the compiler to the corresponding object file,Then the linker looks for the corresponding header file in the link library,最后生成可执行文件,There can be more than one source file here.c文件,可能有多个.c文件.
In practice we can see that,After the file is compiled and run,会生成对应的.exe文件,如下:
我们可以看到,When we don't compile and run,in the project folder only.c源文件,And when we compile and run,You will find one more in the folderDebug文件夹,There are compiled files in it.exe文件;
图2
图3
图4
Each source file will be generated by the compiler to a corresponding object file during the compilation process,as shown in Figure 3test.ccorresponding is generatedtest.obj文件,And each object file(同一个项目中的)All are bundled together by the linker to form a single and complete executable program,正如图4的test_8_3.exe(The executable program generated by the project),连接器同时也会引入标准C函数库中任何被该程序所用到的函数,而且它可以搜索程序员个人的程序库,将其需要的函数也链接到程序中.
Of course this is just a general statement,Actually compiling and linking can be divided into four steps.
图5.Compile and link specific operations
After decoupling compilation and linking,In fact, it is divided into the above four steps,And each step will generate a different file;
但是VS是集成开发环境,Hard to observe details,So we can only be thereLinuxObserve the details above.
比如我们在Linux上写上如下代码:
预处理细节
When we want to observe what happens after preprocessing,We should enter like this
图6 test.i
The code that appears is actually the header filestdio.h中所有的函数.
当然,Preprocessing doesn't just stop there,它还会将All comments removed,并且将#defineSymbols are converted to corresponding content.就比如我#define MAX 10000;那么当我在define下面使用MAX的时候,所有MAX会被替换成10000,当我#define sum 3+2;那么下面使用sum的时候,所有sumwas replaced with 3+2;Then the compiler will#defineDelete that line(Because it has been replaced).
编译细节
To see compilation details we should entergcc -S test.c
The compiler will then generate a file named test.s的文件.
打开test.s文件我们可以发现,It's all assembly code,在编译阶段,The compiler does the syntax analysis,词法分析,语义分析,Symbol summary, etc,The most important of these is the symbolic summary.
符号汇总
Symbolic summarization converts global variables,定义的函数(包括main函数) 进行汇总;
Next comes the assembly stage;
Assembly details
之后再输入gcc -c test.c
生成了test.o文件
This file is stored in binary,Internal stuff is binary stuff;
So we understand that assembly converts assembly code to binary code
如图7
图7
One of the operations in assembly is to form a symbol table;
This is actually related to the symbol summary in the compilation,我们test.cCompiled to generate the correspondingtest.s文件后,The compiler will summarize the corresponding symbols,Then in the assembly link,Generate a symbol table,在这个环节,The compiler will give each symbol in the symbol table an address,用来访问,就比如这里的test.cAfter the file has been assembled,会给Add,g_val,mainThree symbols one address,当然,如果Add函数或者g_valVariables are declared from external files,The symbol table will give them an invalid address,之后在The link stage uses the symbol table.
link details
When all source files are followedelfThe format generates the corresponding.o文件后,编译器会给每一个.o文件按照elfformat generates a segment table,Each empty bit of the segment table stores the corresponding address,Segment table merging is then performed during the linking phase,It will merge symbols of the same name,Take the effective address in the symbol table and put it in the corresponding symbol table,This symbol table is to determine whether the executable program can use this function in the future,If there is an invalid address in the symbol table,cannot use this function,导致链接错误.
If no error occurs during this series of operations,An executable program was successfully generated.
边栏推荐
- Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
- 优炫数据库的单节点如何转集群
- Physical backup issues caused by soft links
- dmp (dump) dump file
- 链表的简单描述及代码的简单实现
- 1667. 修复表中的名字
- [C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
- HDU 1114:Piggy-Bank ← 完全背包问题
- QStyle平台风格
- C student management system head to add a student node
猜你喜欢
Principle and Technology of Virtual Memory
QT language file production
VSCode Change Default Terminal how to modify the Default Terminal VSCode
Gantt chart is here, project management artifact, template is used directly
QT语言文件制作
金仓数据库如何验证安装文件平台正确性
In 2022, you still can't "low code"?Data science can also play with Low-Code!
Access Characteristics of Constructor under Inheritance Relationship
lua learning
How to sort multiple fields and multiple values in sql statement
随机推荐
C student management system Insert the student node at the specified location
Error: Not a signal or slot declaration
Tencent Cloud [Hiflow] New Era Automation Tool
VSCode Change Default Terminal how to modify the Default Terminal VSCode
[Fortune-telling-60]: "The Soldier, the Tricky Way"-2-Interpretation of Sun Tzu's Art of War
QT: The Magical QVarient
解决connect: The requested address is not valid in its context
word分栏小记
02 [Development Server Resource Module]
In 2022, you still can't "low code"?Data science can also play with Low-Code!
How OpenGL works
Error: Not a signal or slot declaration
View handler 踩坑记录
Use @Mapper to query the partition status of oracle and report an error
(十一)元类
【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
1484. Sell Products by Date
dmp(dump)转储文件
倒计时 2 天|云原生 Meetup 广州站,等你来!
How to solve the error cannot update secondary snapshot during a parallel operation when the PostgreSQL database uses navicat to open the table structure?