当前位置:网站首页>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 .
边栏推荐
- 毕业从事弱电3个月,我为什么会选择转行网络工程师
- 【单细胞高级绘图】07.KEGG富集结果展示
- [today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
- SecureCRT garbled code solution [easy to understand]
- Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework
- [today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
- Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?
- test
- [onnx] export pytorch model to onnx format: support multi parameter and dynamic input
- Kubernetes advanced part learning notes
猜你喜欢
![PMP adopts the latest exam outline, here is [agile project management]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
PMP adopts the latest exam outline, here is [agile project management]

Advantages of network virtualization of various manufacturers
![[onnx] export pytorch model to onnx format: support multi parameter and dynamic input](/img/bd/e9a1d3a2c9343b75dbae5c7e18a87b.png)
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input
![[today in history] July 1: the father of time-sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world](/img/41/76687ea13e1722654b235f2cfa66ce.png)
[today in history] July 1: the father of time-sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world

程序的编译和运行

Go language go language built-in container

ROS_ Rqt toolbox

Increase swap space

Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
![[advanced mathematics] [1] function, limit, continuity](/img/c5/f9fd3814a61d0fba24b37253c7e51c.png)
[advanced mathematics] [1] function, limit, continuity
随机推荐
网络RTK无人机上机测试[通俗易懂]
2022.7.24-----leetcode.1184
Online XML to JSON tool
Working principle of radar water level gauge and precautions for installation and maintenance
【TensorRT】trtexec工具转engine
securecrt乱码解决方法[通俗易懂]
Go language go language built-in container
Behind every piece of information you collect, you can't live without TA
Increase swap space
Arrow parquet
What is cluster analysis? Categories of cluster analysis methods [easy to understand]
JS scope and scope chain
CarSim simulation quick start (XV) - ADAS sensor objects of CarSim sensor simulation (1)
Fanoutexchange switch code tutorial
[matlab] download originality documents based on oil monkey script and MATLAB
网络爬虫原理解析「建议收藏」
Docker builds redis cluster
Introduction to several scenarios involving programming operation of Excel in SAP implementation project
JMeter - interface test
Aircraft PID control (rotor flight control)