当前位置:网站首页>Ifndef define ENDIF prevents header files from being included repeatedly. You don't really understand
Ifndef define ENDIF prevents header files from being included repeatedly. You don't really understand
2022-06-12 12:09:00 【Just want to call Yoko】
notes : The following environments are VS2005, Due to my limited programming ability and expression ability , If you don't understand something, you can read it several times , Please point out any mistakes
Here are some basic knowledge , I believe most people already know the following points , You can also skip to the last part #ifndef#define#endif The real role of
1. The precompile phase puts all #include ”***.h“ (“” And <> I won't tell you the difference here ) use ***.h The content of Replace 了 , So there was no .h All .h The contents of have been included in the .cpp in ( notes : Personally, this step takes place in the precompile phase )
2. Generate the last exe File is compiled by 、 Linking is done in two steps , Compilation is source code generation obj The process of binary object file , Notice a source code file ( finger .cpp, Instead of .h, .h Has been included in .cpp It's in ) Generate a obj file , stay VS2005 Compile a separate obj The method to select the .cpp file ctrl+f7, In this article, the following compilation methods are implemented according to this method instead of F7, Since compilation is independent , So there can be functions with the same name in two independent compilation units , for example a.cpp There can be one of them void fun(); b.cpp There can be one of them at the same time void fun(); This is very important , You can try it and understand it clearly
3. During compilation , We can use what we declare , Without its definition , Declarations can be repeated , extern During compilation, you tell the compilation unit that the definition of the variable is in other compilation units , Equivalent to declaration , When linking , There is only one definition in the whole program , For example, the following code , Compilation can be done through , But the link failed
4. Let's analyze it from the perspective of grammar #ifndef#define#endif( I believe everyone on earth knows this )
Precompile phase , When the code is executed for the first time ( namely #include "a.h", See article 1 ) when , Since we have no macro definitions A_H_, So it will execute #define A_H_ as well as void fun() Two statements , The second time the code is executed, it is because #ifndef A_H_ Just go straight to #endif The latter is equal to this time #include "a.h" Nothing has been done
summary :
Okay , Here is a summary of what we have learned above , To correct what people have been saying about #ifndef#define#endif The misunderstanding of
When we have a simple project There are three files in main.cpp, a.cpp, a.h, and main.cpp and a.cpp They respectively contain a.h, In the compilation phase , Both compilation units will contain a.h Of , Even if they use #ifndef#define#endif, That's why when a.h When it is contained in multiple files, we do not allow it in a.h The reason for defining variables and functions in , Because there will be redefinition in the link phase . But in a.h Define a static Variables are allowed , because static Variables are modular scopes , In this case , If we are in a.h Write in static int sss = 0; that main.cpp And a.cpp The use of sss Will be for 2 Independent sss.
So whether #ifndef#define#endif It's useless , You can think about it , When we a.cpp There are many #include "a.h" when , If we use #ifndef#define#endif Then the precompile phase will contain only one a.h To a.cpp in , You might say , Who would be stupid enough to a.cpp Write more than one #include "a.h" Well , So please consider a slightly more complicated situation , When we main.cpp It contains a.h and b.h, and a.h We also include b.h, So if we use #ifndef#define#endif be main.obj Only one copy will be included b.h
边栏推荐
- 宏编译 预处理头 WIN32_LEAN_AND_MEAN
- Elk construction guide
- Preliminary study on Regional Simulation of crop model
- MVC mode, encryption, jsonwebtoken
- Linear model of machine learning
- 屏蔽不显示VS警告warning
- Chaîne la plus longue sans caractères dupliqués (leetcode 3)
- Autolock solves the problem of forgetting to unlock after locking
- LeetCode 497. Random points in non overlapping rectangles (prefix and + bisection)
- Getting started with NVIDIA Jetson nano Developer Kit
猜你喜欢
![[foundation of deep learning] learning of neural network (4)](/img/8d/0e1b5d449afa583a52857b9ec7af40.png)
[foundation of deep learning] learning of neural network (4)

TinyMCE realizes automatic uploading of pasted pictures

Dom+js+ carousel map + no time

A. Prefix range

Ros- resolve error "tf2\u buffer\was not declared in this scope"

Redis的主从复制原理

QML学习 第一天

QML first day

Asynchronous path processing
![[foundation of deep learning] back propagation method (1)](/img/0b/540c1f94712a381cae4d30ed624778.png)
[foundation of deep learning] back propagation method (1)
随机推荐
ARP protocol data processing process of neighbor subsystem
Longest string without duplicate characters (leetcode 3)
Clone with cloneNode to solve the id problem / methods deep copy and shallow copy to modify the ID
Multiplication instruction of arm instruction set
Channel shuffle class
Promise understanding has used promise to realize picture preloading (sequential loading)
Pseudo instruction of arm instruction set
Load/store instruction addressing mode of arm instruction set (2)
ACE配置IPv6, VS静态编译ACE库
Ace configures IPv6, vs statically compiles ace Library
[译] Go语言测试进阶版建议与技巧
传统的DOM渲染方式?
[转]placement new
Create servlet project
Data processing instruction addressing method of arm instruction set
TinyMCE series (IV) introduction to common built-in UI components of TinyMCE
Elk construction guide
About message
Reasons for SSL introduction and encryption steps
Kotlin扩展函数实现原理