当前位置:网站首页>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; }
边栏推荐
- Solve prime numbers between 100 and 200
- 图书馆和档案馆的职能
- 「软件测试」包装简历从这几点出发,直接提升通过率
- 进程的调度
- Redis installation and operation (Linux)
- Uni app wechat applet search keywords are displayed in red
- OSPF总结(思维导图)
- pyqt5使用pyqtgraph画动态散点图
- LeetCode刷题——NO.238——除自身以外数组的乘积
- Encyclopedia of websites commonly used by people who know current affairs
猜你喜欢

Risc-v tool chain compilation notes

图书馆和档案馆的职能

在腾讯测试岗干了5年,7月无情被辞,想给还在划水的兄弟提个醒.....

c语言:深度学习递归

White box test case design (my grandfather can understand it)

【用C语言绘制谢尔宾斯基三角形】

Is it useful to lie down with your eyes closed when you can't sleep?

Hcip day 6 OSPF static experiment

Web3.0世界知识体系分享-什么是Web3.0

GoatGui邀你参加机器学习研讨班
随机推荐
F8 catch traffic, F9 catch rabbits, f10turttle
com.fasterxml.jackson.databind.exc.InvalidDefinitionException
Is it necessary to open an account on site for securities speculation? Is it safe to open an account online
信息收集-端口扫描工具-Nmap使用说明
PPOCRLabel格式的数据集操作总结。
N methods of SQL optimization
平成千字文(へいせいせんじもん) (平成12年9月10日 石渡 明 作) 宇宙広遠 銀河永久 日月運行 不乱無休 地球公転 季節変移 黄道星座 太陽年周 故郷群島 南熱北冷 海洋温暖 気候順良 青空飛雲 諸野深緑 湖泉静息 谷川清流 春桜一面 新芽
JUC concurrent programming
消息队列学习 -- 概念
uni-app上自定义微信小程序的tabbar
If you want to thoroughly optimize the performance, you must first understand the underlying logic~
Towhee 每周模型
[brother Yang takes you to play with the linear table (4) - chain queue]
The latest JD SMS login + silly girl robot nanny level deployment tutorial (July 24, 2022)
猜拳小程序 基于Object-C 新手上路
项目时区问题解决
最新多线程&高并发学习资料,面试心里有底气
bp 插件临时代码记录
证券公司哪家手续费最低?手机上开户安全吗
c语言:深度学习递归







