当前位置:网站首页>Compilation and operation of program
Compilation and operation of program
2022-07-25 20:34:00 【Smart Knight】
Catalog
Edit one 、 Program translation environment and execution environment
1.C There are two environments for language code to run
2. One C Compilation and running process of program
Four 、 The execution environment of the program
One 、 Program translation environment and execution environment
1.C There are two environments for language code to run
- The first 1 One is the translation environment , In this environment, source code is converted into executable machine instructions .
- The first 2 One is the execution environment , It's used to actually execute code .
2. One C Compilation and running process of program
- Each source file constituting a program is converted into object code through the compilation process (object code).
- Each target file is linked by a linker (linker) Tied together , Form a single and complete executable program .
- Linkers also introduce standards C Any function in the function library used by the program , And it can search the programmer's personal library , Link the functions it needs to the program

Two 、 compile
Compilation is divided into preprocessing 、 Compilation and assembly are three processes
The following program contains two files , I will explain the compilation process of this program in detail
test.c
int Add(int a, int b)
{
return a + b;
}function.c
#define A 20
#include<stdio.h>
int b = 10;
int main()
{
int a = A;
int c = Add(a, b);
printf("%d", c);
// Addition operation
return 0;
}1. Preprocessing
For details of preprocessing instructions, see :(9 Bar message ) Preprocessing instruction _ Smart Knight blog -CSDN Blog
Preprocessing will perform a series of operations on the code to facilitate subsequent compilation , Generate .i file
The program mainly completes three tasks in the preprocessing stage : The header file contains 、 Delete all #define And replace all macros and identifier constants 、 Delete all conditional compilation instructions 、 Delete all comments 、 Add line number and file ID 、 Retain #pragma Instructions
The effect is as follows :
test.c
int Add(int a, int b)
{
return a + b;
}function.c
…… Here is stdio.h The content of
#define Be deleted
int b = 10;
int main()
{
int a = 20;A Be replaced
int c = Add(a, b);
printf("%d", c);
The following comments have been deleted
return 0;
}2. compile
Lexical analysis of the preprocessed code 、 Syntax analysis 、 Semantic analysis 、 Symbol aggregation and subsequent optimization produce the corresponding assembly language , Generate .s file .

Here we mainly talk about symbol summary , Symbol summary is mainly about the function names in the preprocessing code 、 The global variables are counted and made into a table .
test.o | function.o |
b( Global variables ) | Add( Function name ) |
main( Function name ) | |
Add( Function name ) |
When you link later, you can check whether the function is correctly defined through this table
3. assembly
Convert assembly language into binary instructions , Generated at this time .o Only the computer can understand the document
The resulting test.o and function.o
3、 ... and 、 link
1. link
In the link phase, two operations, merging segment table and symbol summary, will be performed to form an executable program .
Merge segment tables : The compiler will combine the data in the same format in multiple object files generated in the assembly stage , Finally form an executable program .
In the process of compiling Links , The change of file suffix is different :
- stay windows In the system , Changes in documents :.c -> .obj -> .exe( The three steps of compilation are usually completed in one step )
- stay linux In the system , Changes in documents :.c -> .i ->.s -> .o -> .out
2. Symbol summary
Exists in even one table Add Function name , The linker will confirm which position is the definition of the function , If not , The compilation of this program will make mistakes . therefore , Linking is a step that the machine can check whether a function is undefined .
test.o | function.o |
b( Global variables ) | Add( Function name )( After checking, this is the definition ) |
main( Function name ) | |
Add( Function name ) |
Four 、 The execution environment of the program
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 , The loading of the program must be arranged manually , 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), 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 .
边栏推荐
- 【TensorRT】动态batch进行推理
- [cloud native] use of Nacos taskmanager task management
- Arrow 之 Parquet
- The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
- 移动web布局方法
- Dataframe first performs grouping operation and then combines output
- What is cluster analysis? Categories of cluster analysis methods [easy to understand]
- Prescan quick start to master the special functions of prescan track editing in lecture 18
- CarSim simulation quick start (XIV) - CarSim Simulink joint simulation
- Has baozi ever played in the multi merchant system?
猜你喜欢

ROS_ Rqt toolbox
![[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay](/img/18/b06e2e5a2f76dc2da1c2374b8424b3.png)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
![[advanced mathematics] [4] indefinite integral](/img/4f/2aae654599fcc0ee85cb1ba46c9afd.png)
[advanced mathematics] [4] indefinite integral

Notes - record a cannotfinddatasourceexception: dynamic datasource can not find primary datasource problem solving

Interpretation of filter execution sequence source code in sprigboot

The database empties the table data and makes the primary key start from 1
![Vulnhub | dc: 6 | [actual combat]](/img/7e/de7d5b56724bde5db2bb8338c35aa8.png)
Vulnhub | dc: 6 | [actual combat]

【高等数学】【6】多元函数微分学

Working principle of radar water level gauge and precautions for installation and maintenance

Socket error Event: 32 Error: 10053. Connection closing...Socket close
随机推荐
股票软件开发
MySQL date [plus sign / +] condition filtering problem
网络爬虫原理解析「建议收藏」
每条你收藏的资讯背后,都离不开TA
103. (cesium chapter) cesium honeycomb diagram (square)
Proxy实现mysql读写分离
[advanced mathematics] [1] function, limit, continuity
文件操作详解
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input
Formatdatetime explanation [easy to understand]
Web crawler principle analysis "suggestions collection"
JMeter - interface test
网络协议:TCP Part2
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
Why did I choose to become a network engineer after graduating from weak current for 3 months
"Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
Redis source code -ziplist
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
Network RTK UAV test [easy to understand]
第六章 修改规范(SPEC)类
