当前位置:网站首页>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


 

原网站

版权声明
本文为[Just want to call Yoko]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121206218395.html