当前位置:网站首页>C program compilation and predefined detailed explanation
C program compilation and predefined detailed explanation
2022-08-04 01:53:00 【Waves of rain 123】
Program compilation and execution environment
c/c++Different from other programs running on the virtual machine language,c/c++在编写完代码,点击运行时,This code in a compiler after precompiled first,编译,汇编,Link process to generate an executable program file,后缀为.exe,And then to write executable file into memory,A program running.This article we discuss these a few simple process of what had happened,The final executable is how.
预编译
In the precompiled process,The editor will first delete the comments in the code,Because the annotation is to show the user,Code files don't need these.With the comments removed,Editor program include the header file will be imported into the content of the statement of the program,,And then will complete the replacement of predefined symbols,如将 #define中的内容替换,这些工作完成之后,Will generate a suffix for .obj 的文件
编译
在编译这个阶段,It will be treated with the same front precompiled good code for lexical analysis,语法分析,符号汇总,语义分析,分析完成后,Then analysis complete code into assembly code.Assembly code to machine code is a one-to-one,无论哪种编程语言,To execute the code in the compiler will convert assembly code,Then the assembly code into machine code.But during the compilation of this stage,Haven't convert assembly code into machine code,This is the assembly stage the do.
汇编
In the assembly at this stage,Is to compile phase formed in front of the assembly code into machine code,And will sign in front of summary is expressed as a symbol table,到这里,The code to handle the work done even,Then the remaining links to this stage,A diagram can better understand the following links to phase.
链接
In the general development projects,会有多个源文件,Each source file will contain some of the same header file,And link in front of this stage is to deal with good source file for a summary,Will be different in the source file symbol table summarize,Remove the same only keep an effective,And then to link library code extraction need,Eventually form the executable file.Why say to link library extraction,If it is our full implementation of their own header file,There is no need to like link library extraction,But as the reference standard header file,例如<stdio.h>里的printf,scanf等,The function of the code is in the library,Only to link extraction,才能够使用.Of course this process is not so simple,这里只是简单描述一下,Want to go deeper into the can reference books《程序是怎样跑起来的》,《程序员的自我修养》
预定义
A brief introduction of application of the executable file is how to generate,Then we returned to the predefined
预定义符号
The above is the basic of predefined symbols,But the compiler has defined,We don't have to repeat definition,Holding can be use,The predefined operators at the time of writing log is used
预定义宏
In defining the macro when naming rules is a macro name general in capital letters,Different from other symbol
宏的本质是替换,Therefore when using a macro definition,Careless slightly can produce ambiguity,为了避免产生歧义,Be about to use more brackets,To assign priority.
看下面一个例子
#define SQARE(x) x*x
int main()
{
int r = SQARE(5+1)
}
#define SQARE(x) ((x)*(x))
int main()
{
int r = SQARE(5+1)
}
//仔细看上面的代码,How much is the calculate their respective value
为什么会这样呢?The essence of the predefined is replaced,We have replaced numerical past try
第一个 m = 5 + 1 * 5 + 1
第二个 n = ((5+1) * (5+1))
Replacement arithmetic of the reentry after found the problem,After the first to replace,Because of operational priority problem,产生了歧义,Cause the program did not get the result we want,So don't skimp parentheses when predefined.
一个练习题:写一个宏,Used to calculate structure for a member relative to the starting position of the offset
Think about look at the following code
#include<stdio.h>
#define STRUCTSIZE(STRUCTTYPE, MBERNAME) \
(int)&(((STRUCTTYPE *)0)->MBERNAME) //宏定义的实现
struct S
{
char a;
int b;
float c;
} s;
int main()
{
printf("%d", STRUCTSIZE(struct S, b));
return 0;
}
//思路:First is the number0Forced into required structure pointer type
那么这个0Is the structure pointer type0地址,The offset of each member of the structure,Is essentially the original position relative to the structure of address,We use the starting address for0Structure pointer dereferencing the internal members,And then take the members of the address castint 类型,Expressed to offset
#与##的用法
#The role of is going to pass the parameters in the form of a string of said
举个例子
//在这之前需要了解
printf("hello ""word\n");
printf("hello world\n");
//这两种写法是等价的,printfWill be more groups of string into a string of
#include<stdio.h>
#define PRINT(FORMAT, VALUE) \ //这里'\'The offset line
printf("the value is "FORMAT, VALUE)
int main()
{
int a = 1;
int b = 2;
printf("the value is %d\n", a);
printf("the value is %d\n", b);
PRINT("%d\n", a);
PRINT("%d\n", b);
return 0;
}
//The code segment plays a role of matting program
#include<stdio.h>
int main()
{
int a = 1;
int b = 2;
printf("the value of a is %d\n", a);
printf("the value of b is %d\n", b);
return 0;
}
//看上面的程序,除了a,b的不同,其他都是相同的,So we can
printf("the value of a is %d\n", a);
printf("the value of b is %d\n", b);
//封装成一个函数,Only need to input a parameter can achieve the same output
//但事实上,函数是无法实现的,Because there is no way to show function"the value of a is %d\n"中的 a,
"the value of b is %d\n"中的 b ,And with the macro definition can realize,#就派上了用场
#include<stdio.h>
#define PRINT(VALUE) \
printf("the value of "#VALUE" is %d\n", VALUE)
int main()
{
int a = 1;
int b = 2;
PRINT(a);
PRINT(b);
return 0;
}
//#Will replace the parameters in the form of a string that
//The above procedure to replace the results are as follows
1.The first will parameters VALUE 替换成a
printf("the value of "#VALUE" is %d\n", VALUE)
printf("the value of " "a" " is %d\n", a)
2.The second will parameters VALUE 替换成b
printf("the value of "#VALUE" is %d\n", VALUE)
printf("the value of " "b" " is %d\n", b)
##的作用是将##On both sides of the string together
举个栗子
#include<stdio.h>
#define FUN(VALUE1, value2) VALUE1##VALUE2
int main()
{
int langyu = 10;
printf("%d ", FUN(lang, yu));
return 0;
}
//FUN会将lang 和 yu结合在一起,形成langyu,Program the end result will print10
The difference between the function and predefined
#include<stdio.h>
#define SQARE(x) ((x)*(x))
int Sqare(int m)
{
return m*m ;
}
int main()
{
int r = SQARE(5+1);
int n = Sqare(5+1);
printf("%d %d", r, n);
return 0;
}
1.Macro definition is directly replace when processing,And at the time of the processing function definition to a stack up space in memory,The more memory than macro definition
2.函数栈帧的创建,In theory to perform more instructions,Macro definition on speed also is better
3.The symbol of a macro definition can use other macro definition,But can't recursive call,Also is not able to call themselves
4.For more complex functions,Using the function is more convenient,But there are some functions have no way to realize,The predefined can be very good,参考上面的#的使用
5.Macro definition not to check the parameters,Function of more stringent inspection,不容易出错
6.宏是没法调试的
条件编译
在编写代码时,There may be a piece of code is used to test the results,如果不删,Feeling a little out of the way,But deleted the next time debugging to rewrite again,This time can consider to use conditional compilation,Meet the conditions to compile,Does not meet the condition will not attend compilation,Below is a list of some common conditional compilation
1.Single conditional compilation
#if 常量表达式
//...
#endif //如果常量表达式为真,Is in the middle of the code in translating,若为假,则不参加编译
2.多分支条件编译
#if 常量表达式
//...
#elif 常量表达式
//...
#else
//...
#endif //Which branch of the constant expression is true,The code under branch in translating
3.判断是否被定义
#define X 10
#if define(X) //#ifdf X The writing can be flat for
//...
#endif //如果X被定义了,Then in the middle of the code in translating,如果X未被定义,则不参与编译
#if !define(X) //#ifndf The writing can be flat for
//...
#endif //如果X未被定义,The code is run in the middle,If it be defined,则不运行
4.嵌套指令
#if defined(X)
#ifdef OPTION1
//...
#endif
#ifdef OPTION2
//...
#endif
#elif defined(Y)
#ifdef OPTION2
//...
#endif
#endif
//Conditions of predefined operators can use nested
文件包含
In the header file contains,We usually have two contains the way,一种是用 " " 来包含
21 kind is with< > 来包含,So what is the difference between this two contains forms?
Both contain way find file is the main difference between the strategy of different
" " This way of containing the compiler will first find the program files in the directory,If you do not find will go to the library catalog to check
<>This way of containing the compiler will directly go to the library directory search
例如,标准库文件stdio.h就用<>来包含,The compiler will directly go to the library files to find
包含头文件后,The compiler will at a precompiled stage to find header files,With the one in the file will contain the information content,If repeat include the header file at this time,Causes include content repeat,Invalid code quantity increase
Maybe you will feel how could repeat contains a header file,Repeat contains more in people development
So how to avoid repeating contains
有两种方法
1.较为繁杂
2.较为简便
方法一
#ifndef __TEST_H__
#define __TEST_H__
//头文件的内容
#endif //__TEST_H__
//The annotation to include the header file on the middle area
方法二
#pragma once
//At the beginning of the file location can be write
边栏推荐
- 持续投入商品研发,叮咚买菜赢在了供应链投入上
- 网页三维虚拟展厅为接入元宇宙平台做基础
- C语言力扣第54题之螺旋矩阵。模拟旋转
- Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
- 持续投入商品研发,叮咚买菜赢在了供应链投入上
- Slipper - virtual point, shortest path
- How to copy baby from Taobao (or Tmall store) through API interface to Pinduoduo interface code docking tutorial
- halcon自定义函数基本操作
- 实例037:排序
- html select tag assignment database query result
猜你喜欢
Security First: Tools You Need to Know to Implement DevSecOps Best Practices
Array_Sliding window | leecode brushing notes
OpenCV如何实现Sobel边缘检测
Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
lombok注解@RequiredArgsConstructor的使用
参加Oracle OCP和MySQL OCP考试的学员怎样在VUE预约考试
循环绕过问题
MySQL回表指的是什么
Example 037: Sorting
什么是SVN(Subversion)?
随机推荐
C程序编译和预定义详解
Example 037: Sorting
实例040:逆序列表
如何用C语言代码实现商品管理系统开发
Installation and configuration of nodejs+npm
C语言:学生管理系统(链表版)
实例035:设置输出颜色
Example 041: Methods and variables of a class
boot issue
Flask框架初学-05-命令管理Manager及数据库的使用
this巩固训练,从两道执行题加深理解闭包与箭头函数中的this
idea中diagram使用
Apache DolphinScheduler actual combat task scheduling platform - a new generation of distributed workflow
Summary of GNSS Articles
Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
Slipper —— 虚点,最短路
Instance, 038: the sum of the diagonal matrix
Engineering drawing review questions (with answers)
循环绕过问题
Variable string