Original address: http://blog.csdn.net/q191201771/article/details/6399820
Here to explain the next few basic knowledge, I believe that most people for the following points are already known, you can also jump to the last part of the #ifndef#define#endif to see the real role
1. In the precompiled phase, all #include "***.h" ("" and <> differences are not mentioned here) are replaced with ***.h content. And then there's No. h all of the contents of. h are included in the. cpp that requires them (note: This step is personally considered to occur during the precompilation phase)
2. The final EXE file is generated by compiling, link two-step completion, the compilation is the source code generated obj binary target file process, Note that a source code file (a . cpp, rather than. h,. h has been included in. cpp) generates an obj file, The way to compile an obj separately in VS2005 is to select the. cpp file Ctrl+f7, which is executed in the following way rather than F7, because the compilation is independent, so there can be duplicate functions in two separate compilation units, For example a.cpp can have a void fun (); A void fun () can be b.cpp in the same time; This is very important, you can try and understand
3. During the compilation, we just declare what we can use, without its definition, the declaration can be repeated, and extern at compile time tells the compilation unit that the definition of the variable in another compilation unit, equivalent to the Declaration, link, defined in the entire program has and only a copy, such as the following code, compilation can be passed, but the chain Failed at the time of the connection
[CPP] View plain copy extern int A; extern double a;//error, a can have only one type of extern int A; extern void fun1 (int a, int b); extern void fun1 (int a, int b); extern void fun1 (int a, int b, int c);//function overload void fun2 (); void fun2 (int a);//function overload void Fun2 () {} int main () {a = 100; FUN1 (200, 300); Fun2 (); FUN2 (100); return 0; } void Fun2 ();
4. We analyze the #ifndef#define#endif from the grammar (this is believed by the Earth people know)
[CPP] View plain copy//-----a.h-----#ifndef a_h_ #define A_H_ void Fun (); #endif
In the precompiled phase, when the code is executed for the first time (that is, #include "a.h", see the first), because we do not have a macro definition a_h_, the #define A_h_ and void fun () Two statements are executed, and the second time the code is executed because of the #ifndef A_h_ If you go straight to the #endif, it's the #include "a.h."
Summary:
Well, here's what we've learned to sum up, to correct everyone's misunderstanding of #ifndef#define#endif.
When we have three files in a simple project main.cpp, A.cpp, A.h, and main.cpp and A.cpp contain a.h, in the compilation phase, two compile units will contain A.H, even if they use #ifndef# DEFINE#ENDIF, which is why we are not allowed to define variables and functions in A.H when A.H is included in multiple files, because redefinition occurs during the link phase. But defining a static variable in A.H is permissible because the static variable is a modular scope, and for this example, if we write the static int sss = 0 in the a.h, then the a.cpp used by main.cpp and SSS will be 2 separate SSS.
Then whether the #ifndef#define#endif is useless, you can think about, when we A.cpp wrote a number of #include "a.h", if we use the #ifndef#define# endif so the precompilation phase will only contain the content of a a.h into the a.cpp, you might say, who would be stupid enough to write #include "a.h" in A.cpp, consider a slightly more complex situation when we main.cpp include A.H and B.h, And a.h we include b.h, then if we use #ifndef#define#endif then Main.obj will only contain a copy of the B.h