当前位置:网站首页>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 .
边栏推荐
- Follow up of Arlo's thinking
- Automated testing ----- selenium (I)
- [today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
- Leetcode customs clearance: hash table six, this is really a little simple
- Clickhouse notes 02 -- installation test clickvisual
- 【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
- 网络协议:TCP Part2
- Kubernetes advanced part learning notes
- Go language go language built-in container
- How much memory does bitmap occupy in the development of IM instant messaging?
猜你喜欢

Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released

移动web布局方法

Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP

「分享」DevExpress ASP.NET v22.1最新版本系统环境配置要求

【单细胞高级绘图】07.KEGG富集结果展示
![[cloud native | learn kubernetes from scratch] VIII. Namespace resource quotas and labels](/img/7e/2bdead512ba5bf5ccd0830b0f9b0f2.png)
[cloud native | learn kubernetes from scratch] VIII. Namespace resource quotas and labels

毕业从事弱电3个月,我为什么会选择转行网络工程师

During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?

How much memory does bitmap occupy in the development of IM instant messaging?

Docker 搭建 Redis Cluster集群
随机推荐
接口请求合并的3种技巧,性能直接爆表!
Fanoutexchange switch code tutorial
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
[MCU] 51 MCU burning those things
Log in to Baidu online disk with cookies (websites use cookies)
[noi simulation] string matching (suffix automata Sam, Mo team, block)
Redis source code -ziplist
Online XML to JSON tool
【云原生 | 从零开始学Kubernetes】八、命名空间资源配额以及标签
Introduction to several scenarios involving programming operation of Excel in SAP implementation project
[tensorrt] dynamic batch reasoning
Interpretation of filter execution sequence source code in sprigboot
Go language go language built-in container
[advanced mathematics] [4] indefinite integral
Array of sword finger offer question bank summary (I) (C language version)
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
Aircraft PID control (rotor flight control)
C language file reading and writing
2022.7.24-----leetcode.1184
How much memory does bitmap occupy in the development of IM instant messaging?
