当前位置:网站首页>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; }
边栏推荐
- JS 数组去重(含简单数组去重、对象数组去重)
- 面试必问 | 一个线程从创建到消亡要经历哪些阶段?
- Prometheus operation and maintenance tool promtool (III) debug function
- 使用注解方式实现 Redis 分布式锁
- 平成千字文(へいせいせんじもん) (平成12年9月10日 石渡 明 作) 宇宙広遠 銀河永久 日月運行 不乱無休 地球公転 季節変移 黄道星座 太陽年周 故郷群島 南熱北冷 海洋温暖 気候順良 青空飛雲 諸野深緑 湖泉静息 谷川清流 春桜一面 新芽
- Prometheus 运维工具 Promtool (三) Debug 功能
- JMeter下载安装
- After ten years of testing, I want to say to my friends who are still confused: one thing is to do a good job in personal planning
- Fist guessing applet based on Object-C novice on the road
- 最新多线程&高并发学习资料,面试心里有底气
猜你喜欢

Hcip first day

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

The latest JD SMS login + silly girl robot nanny level deployment tutorial (July 24, 2022)

C语言程序的编译上

Multipoint bidirectional republication and routing strategy topology experiment

从初级程序员到架构师学习路线+配套学习资源完整版

中断、信号、系统调用

【斐波那契数列及螺线 基于C语言】

祝大家七夕快乐,邀你源码共读

OSPF summary (mind map)
随机推荐
线程和进程
无效的目标发行版:17 的解决办法
Greenplum【部署 08】数据库小版本升级流程及问题处理 Error: open-source-greenplum-db-6 conflicts with
How many holes have you stepped on in BigDecimal?
Summary of dataset operations in ppocrlabel format.
图书馆和档案馆的职能
小程序utils
com.fasterxml.jackson.databind.exc.InvalidDefinitionException
见证中国网安力量 “解码2022中国网安强星”即将启航
中断、信号、系统调用
软件测试基础理论知识—概念篇
解决小程序报错getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json
After working in Tencent testing post for 5 years, I was ruthlessly dismissed in July, trying to wake up my brother who was still paddling
Record the star user of handsomeblog
[draw sherpinski triangle in C language]
Plato Farm有望通过Elephant Swap,进一步向外拓展生态
Three handshakes and four disconnects of TCP
Jmeter接口测试, 快速完成一个单接口请求
LeetCode刷题——NO.238——除自身以外数组的乘积
通达信开户安全么






