当前位置:网站首页>Program environment and preprocessing (Part 1): how does a program run successfully?
Program environment and preprocessing (Part 1): how does a program run successfully?
2022-07-27 05:36:00 【vpurple__】
Come on , You can !! You must be familiar with all this !!!
Catalog
1. Program translation environment and execution environment
2. Detailed compilation + link
2.1 The translation environment of the program
2.2 Several stages of compilation
3. Write the last point with emotion
The translation environment of the program
1. Program translation environment and execution environment
stay ANSI C( international standard c) In any implementation of , There are two different environments .
The first 1 Species is Translation environment , In this environment, source code is converted into executable machine instructions ( Binary instructions | Instructions that the machine can read ).
The first 2 Species is execution environment , It's used to actually execute code .
2. Detailed compilation + link
2.1 The translation environment of the program
All the object files plus the link library are processed by the linker and finally the executable program is generated .exe file .
There may be many in a project .c file , Write in modules , Increase of efficiency .
.c Files are called source files .
Each source file goes through the compiler separately ( There is only one compiler ) Process and generate its own corresponding target file
linux The suffix of the target file generated in the environment is .o

Link library :
The library on which the included header file depends , You need a library file to provide , Compile it into the program before it can be used . What the library function depends on will be provided in the link library , Link to the final executable program . That is, in addition to some things we realize ourselves , Some things provided by the compiler .
compiler :
stay vs In the environment It uses cl.exe
The linker :
stay vs In the environment link.exe
.h The file will be copied to .c In file
2.2 Several stages of compilation

In the pretreatment stage
Complete the following tasks
1. The header file contains
#include This kind of instruction should be processed at this stage
2.#define Defines the substitution of symbols
And the defined symbols will be deleted
3. Note delete
……To sum up :
In the preprocessing stage, all we do is text operation
The above tasks are all text operations
In the compilation phase :
gcc test.i -SFor the preprocessing file just generated ,test.i Compile
gcc test.c -S
about test.c Preprocess before compilingDemonstrate options gcc test.i -S
It generates a test.s file ,.s Inside is assembly code
Compile phase
hold c Language code conversion assembly code
During this conversion process :
1. Syntax analysis
2. Lexical analysis
3. Symbol summary
4. Semantic analysisSymbol summary :
It will summarize the global symbols in the code we write
Whether it is the global variable name 、 Function name
Put it all together
In the assembly phase
gcc test.s -c
It generates a test.o file
test.o The object file is in the assembly stage , Convert assembly code into machine instructions ( Binary instructions )Each object file will form a symbol table , Associate symbols with addresses .
In the linking process
1. Merge segment tables
2. Merging and relocating symbol tablesWill all .o Target files and link libraries , Form a file Executable program .
stay linux There is a elf Format
.o Files are binary files
.o The document is based on elf File format to organize the contents of the filelinux The executable program file in the environment is also elf Format
2.3 Running environment
The process of program execution :
1. The program must be loaded into memory .
In an operating system environment , This is usually done by the operating system .
In an independent environment ( For example, single chip microcomputer ), The loading of the program must be arranged manually , It can also be done by putting executable code into read-only memory .
2. Find the entry to the program .
The execution of the program begins , And then I call main function .
3. Start executing program code .
At this point, the program will use a runtime stack (stack)( Function stack frame ), Store function local variables and return address . Programs can also use static (static) Memory , Variables stored in static memory are stored throughout the execution of the program Keep their values all the time .
4. To terminate the program .
Normal termination main function .
It could be an accidental termination , For example, memory leaks 、 The machine is out of power .
3. Write the last point with emotion
I'm here to sincerely sleep for my previous class 、 I was stunned 、 Running God , Because I didn't review in time after class 、 Consider the past you shall know the future 、 Continue to rethink and repent deeply ……
c Language class is over , What I learned in the second half is almost the same , It should be that there was not much in it at the beginning , There was only one sound . Now make up for it 、 I hope it's still time to review in the form of blog ..
Don't mess up this summer vacation , Roll up !!!
边栏推荐
- Find the number of combinations (the strongest optimization)
- Utility gadget: kotlin code snippet
- Numpy data type conversion
- redis锁
- 322 coin change of leetcode
- MQ set expiration time, priority, dead letter queue, delay queue
- C语言入门介绍
- Collation of several difficult methods in pytorch --gather & squeeze & unsqueeze
- 订单系统功能实现
- 笔记系列k8s编排MySQL容器-有状态的容器创建过程
猜你喜欢
随机推荐
The receiver sets the concurrency and current limit
Makefile 通俗易懂详解
李宏毅机器学习组队学习打卡活动day01---机器学习介绍
后台品牌管理功能实现
MQ set expiration time, priority, dead letter queue, delay queue
登录到主页功能实现
GCC compilation options
redis持久化
[codeworks round 801 div2 D tree queries] tree greedy conclusion
初识C语言——常见的数据类型
初始C语言——关键字static的作用
李宏毅机器学习组队学习打卡活动day03---误差和梯度下降
mq常见问题
自己动手做一个爬虫项目
小米商城项目_注册
消息可靠性处理
LeetCode刷题之322 Coin Change
Niuke sword refers to the path in the offer--jz12 matrix
Hi3516DV300环境搭建
flask一对多数据库创建,基础增删改查









