当前位置:网站首页>C language program compilation
C language program compilation
2022-07-27 02:44:00 【A collection of old dreams 186】
author : A collection of old dreams 186
project :C Language programming ---- Little bit's Growth Diary
Daily inspirational :
Human potential is infinite , Make an effort to , Only then do I know my real strength . Because the direction of the tree , Wind determination . The direction of people , Their own decisions .
Preface :
Today, Xiaobian will bring you to know c Compiling and linking the underlying logic of language
Catalog
1. Program translation environment and execution environment
2. Detailed compilation + link
2.2 The compilation itself is also divided into several stages :
3.2.1 #define Define identifier
1. Program translation environment and execution environment
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. Detailed compilation + link
When we write the code , And when mutating and linking , A black box will pop up , In our project directory, we will generate .exe Executable file , What we need to know now is how this thing is formed .
2.1 Translation environment
We can generally know that , The executable is compiled by 、 Link these two parts together to produce . It can be said in words : Source file ( It can be more than one ) After the compiler compiles, the object file is generated , These target files are sent to the link library for linking , Thus, an executable program is generated .
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 individual programmers The library of , Link the functions it needs to the program
2.2 The compilation itself is also divided into several stages :
One thing we need to pay attention to , The compilation process of the compiler is not just compilation . in other words , Compilation itself can also be divided into several stages .
because Visual Studio These compilers are an integrated development environment , Therefore, it is not convenient to show the details of each part . But study later Linux In the process of , We can certainly observe it when we look back . Here I will explain in words .
1. In the precompile phase , The compiler will translate the contents of the header file 、define Defined text substitutions are written .i The file of , And delete the comments . in other words , Precompiled .i It's written in the document C The language code , Is all that includes header files C The language code .
2. Compile phase , From this stage , It is necessary to gradually enable computer hardware to recognize our code . So this stage will put C Translation of language code into assembly language code . For example, we have int max = 10; , Then the compilation phase will put int As a type, generate the corresponding assembly instructions ,max Is a variable name. , Generate the corresponding assembly statement , = It's the assignment symbol , Assembly statements will also be generated , wait …… These assembly codes are stored in .s In file .
3. assembly , Here are the steps to generate the target file . At this stage, the assembly code will be split in more detail , Generate the most primitive machine instructions , Binary instructions . These instructions will be placed in .obj The file of (Linux In the environment .o file ), stay Linux These binary instructions can be observed in the environment , But our brain will think these are garbled .
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 , ProgramThe loading of must be manually scheduled , It can also be done by putting executable code into read-only memory .2. The execution of the procedure begins . And then I call main function .3. Start executing program code . At this point, the program will use a runtime stack (stack), Store the local variables and return of the function 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 .
3. Pretreatment details
3.1 Predefined symbols
__FILE__ // Source files to compile __LINE__ // The current line number of the file __DATE__ // The date the file was compiled __TIME__ // When the file was compiled __STDC__ // If the compiler follows ANSI C, Its value is 1, Otherwise, it is not definedThese predefined symbols are built into the language .
Take a chestnut :
#include<stdio.h> int main() { FILE* p; p = fopen("test.txt", "w"); if (p == NULL) { perror("open"); } // Format output fprintf(p, "file:%s line:%d date:%s time:%s\n", __FILE__, __LINE__, __DATE__,__TIME__); fclose(p); p = NULL; return 0; }
3.2 #define
3.2.1 #define Define identifier
grammar :#define name stuff
#define MAX 1000 #define reg register // by register This keyword , Create a short name #define do_forever for(;;) // Replace an implementation with a more vivid symbol #define CASE break;case // Writing case Automatically put break write . // If you define stuff Too long , It can be divided into several lines , Except for the last line , Add a backslash after each line ( Line continuation operator ). #define DEBUG_PRINT printf("file:%s\tline:%d\t \ date:%s\ttime:%s\n" ,\ __FILE__,__LINE__ , \ __DATE__,__TIME__ )
put questions to :
stay define When defining identifiers , Do you want to add... At the end ; ?
Better not add ;
such as :
#include<stdio.h> #define MAX 100; int main() { int n = 0; scanf("%d", &n); if (n > MAX) { n = MAX; } return 0; }It should be noted that , When we define identifiers , Try not to add a semicolon after it , Because when we write code, we are used to adding semicolons , If there is a semicolon after the defined identifier , There may be imperceptible errors .
![]()
Of course, we can also use this function to realize some elusive operations , Suppose a programmer uses swich In the sentence case , It's all about break, He felt very troublesome , Then he wrote such a piece of code :
#define CASE break;case #include <stdio.h> int main() { int n = 3; switch (n) { case 1: CASE 2 : CASE 3 : printf("hello world!\n"); CASE 4 : break; } return 0; }
边栏推荐
- Dynamically set the height of applet swiper
- 信息收集-端口扫描工具-Nmap使用说明
- hcip--ospf接口网络接口类型实验
- Encyclopedia of websites commonly used by people who know current affairs
- Smooth data migration from single table to sub table
- LeetCode刷题——NO.238——除自身以外数组的乘积
- js utils 零碎
- Fist guessing applet based on Object-C novice on the road
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- 膜拜,阿里内部都在强推的321页互联网创业核心技术pdf,真的跪了
猜你喜欢

Smooth data migration from single table to sub table

Handsomeforum Learning Forum

What is the principle of synchronized lock escalation in multithreading?
![[draw sherpinski triangle in C language]](/img/e6/9d1d088d1c7675c23725443000329b.png)
[draw sherpinski triangle in C language]
![[dimension reduction blow, take you to learn CPU in depth (Part 1)]](/img/59/0c2b8e1a832ef14e2e0b8e8cdcce9c.png)
[dimension reduction blow, take you to learn CPU in depth (Part 1)]

Hcip day 5 OSPF extended configuration experiment

It has been established for 3 years, and now goose factory has an annual income of millions +. As some suggestions of software testing predecessors

Writing a pacesetter frequently -- yunxiaojing

图书馆和档案馆的职能

OSPF summary (mind map)
随机推荐
聊聊自动化测试的度量指标
[brother Yang takes you to play with the linear table (I) - sequence table]
Basic theoretical knowledge of software testing - concept
图书馆和档案馆的职能
使用注解方式实现 Redis 分布式锁
cookie增删改查方法
【无标题】
Rip routing information protocol topology experiment
Hcip day 3 Wan topology experiment
Record the star user of handsomeblog
Go language slow start - package
Record the nth SQL exception
Summary of dataset operations in ppocrlabel format.
见证中国网安力量 “解码2022中国网安强星”即将启航
Dynamically set the height of applet swiper
C language student information management system can access text files based on arrays
I wish you a happy Chinese Valentine's day and invite you to read the source code together
pyqt5使用pyqtgraph画动态散点图
Tabbar of customized wechat applet on uni app
砺夏行动|源启数字化:既有模式,还是开源创新?







