当前位置:网站首页>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

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.
边栏推荐
- 解决端口占用问题 Port xxxx was already in use
- 【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
- C language implements a simple number guessing game
- Error: Not a signal or slot declaration
- Images using redis cache Linux master-slave synchronization server hard drive full of moved to the new directory which points to be modified
- 优炫数据库的单节点如何转集群
- The 22-07-31 weeks summary
- 线上MySQL的自增id用尽怎么办?
- 01 [Foreword Basic Use Core Concepts]
- Programmer's Tanabata Romantic Moment
猜你喜欢
A small tool to transfer files using QR code - QFileTrans 1.2.0.1
leetcode 15
The 2022 EdgeX China Challenge will be grandly opened on August 3
Common hardware delays
数据增强Mixup原理与代码解读
VSCode Change Default Terminal how to modify the Default Terminal VSCode
mysql can't Execute, please solve it
通过模拟Vite一起深入其工作原理
Cloud Native (32) | Introduction to Platform Storage System in Kubernetes
Is your data safe in this hyperconnected world?
随机推荐
Error: Not a signal or slot declaration
协作D2D局部模型聚合的半分散联合学习
Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
Pisanix v0.2.0 released | Added support for dynamic read-write separation
[C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
Simple description of linked list and simple implementation of code
Ant Sword Advanced Module Development
Access Characteristics of Constructor under Inheritance Relationship
The design idea of DMicro, the Go microservice development framework
torch.roll()
mysql树状结构查询问题
Review 51 MCU
Is your data safe in this hyperconnected world?
One hundred - day plan -- -- DAY2 brush
Cloud Native (32) | Introduction to Platform Storage System in Kubernetes
语法基础(变量、输入输出、表达式与顺序语句)
C student management system head to add a student node
Solve connect: The requested address is not valid in its context
J9 Digital Currency: What is the creator economy of web3?
C language implements a simple number guessing game