The difference between defining a variable and declaring a variable is that defining an operation that will generate memory allocation.
/* Module1.h */
Int a = 1;/* define int a */In the. h file of Module 1 */
/* Module1.c */
# Include "module1.h"/* contains the. h file of Module 1 in Module 1 */
/* Module2.c */
# Include "module1.h"/* contains the. h file of Module 1 in Module 2 */
/* Module3.c */
# Include "module1.h"/* contains the. h file of Module 1 in Module 3 */
The above result defines the integer variable a in Modules 1, 2, and 3. a corresponds to different address units in different modules, because once it contains. h, defines a, and the compiler allocates memory for it. In other words, these three a s are different! This design is never needed, so the compiler will capture this phenomenon and tell you that there is already a defined.
Error LNK2005:... already defined in... obj;
Fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe.
'''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
We can see a company's programming rule: "do not define variables in header files", because variables are defined in header files. If they are included in other. C files, they will be repeatedly defined. Isn't there # ifndef, # define, and # endif to prevent repeated inclusion? How can this problem be repeated? Variables are defined in the header file of the C ++ class. wouldn't all be defined repeatedly in this case? I am confused.
# Ifndef, # define, # endif can only prevent a single C/CPP file from repeatedly containing header files
Multiple C/CPP instances are independent and cannot be blocked from each other.
Variables are defined in the header file of the C ++ class. wouldn't all be defined repeatedly in this case?
----------
Variable objects are available only when class objects are defined.
Your company is talking about global variables and so on.
'''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''
Global variables are generally defined in the. cpp file and extern is defined in the header file.
The header file should use the extern keyword to declare the global variable (not defined)
Test. h file
Extern int;
Test. cpp File
# Include "test. H"
Int;
Int main ()
{
}
'''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
1. Two files: A. H, Main. cpp. Main. cpp # include "A. H ":
It is best not to use this method, because it is best not to define variables in the header file:
(1) [A. H]:
Int B; // only one row, defining the int variable
(2) [main. cpp]:
# Include "A. H" // yes, but if other. cpp files reference A. H, the definition is redefined (in multiple. cpp files.
Main (){...}
----------------------------------------------------
2. Based on the above, add a # include "A. H" in [main. cpp], then redefine (in A. cpp ):
[Main. cpp]:
# Include "A. H"
# Include "A. H" // second, error, variable B redefinition
Main (){...}
-------------------
The purpose of the header file Guard is to prevent the redefinition of one. cpp (not multiple. cpp.
Therefore, you can add # ifndef # define # endif in A. H:
(1) [A. H]:
# Ifndef header_a
# Define header_a
Int B; // defines the int variable, but it is best not to do this. Try not to define the variable in the header file.
# Endif
----------------------------------------------------
3. there is only one of the above. CPP references. h, if there are other. CPP references. h, the variable B is redefined, and an error occurs. therefore, you can set B as a global variable (with extern), that is, only declare B, so that multiple. CPP can be accessed, and then in. CPP defines:
(1) [A. H]:
# Ifndef header_a
# Define header_a
Extern int B; // declare the int variable
# Endif
(2) [A. cpp]:
# Include "A. H"
(3) [main. cpp]:
# Include "A. H"
# Include "A. H" // yes, because there is a header file guard
Int B = 3; // you can define B in A. cpp, but only one. cpp can be selected in two. cpp and B can be defined.
Main (){...}
'''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''
Global variables are generally defined in the. cpp file and extern is defined in the header file.
The header file should use the extern keyword to declare the global variable (not defined)
Test. h file
Extern int;
Test. cpp File
# Include "test. H"
Int;
Int main ()
{
}
'''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
1. Two files: a. h, main. cpp. main. cpp # include "a. h ":
It is best not to use this method, because it is best not to define variables in the header file:
(1) [a. h]:
Int B; // only one row, defining the int variable
(2) [main. cpp]:
# Include "a. h" // yes, but if other. cpp files reference a. h, the definition is redefined (in multiple. cpp files.
Main (){...}
----------------------------------------------------
2. Based on the above, add a # include "a. h" in [main. cpp], then redefine (in a. cpp ):
[Main. cpp]:
# Include "a. h"
# Include "a. h" // second, error, variable B redefinition
Main (){...}
-------------------
The purpose of the header file Guard is to prevent the redefinition of one. cpp (not multiple. cpp.
Therefore, you can add # ifndef # define # endif in a. h:
(1) [a. h]:
# Ifndef HEADER_A
# Define HEADER_A
Int B; // defines the int variable, but it is best not to do this. Try not to define the variable in the header file.
# Endif
----------------------------------------------------
3. there is only one of the above. CPP references. h, if there are other. CPP references. h, the variable B is redefined, and an error occurs. therefore, you can set B as a global variable (with extern), that is, only declare B, so that multiple. CPP can be accessed, and then in. CPP defines:
(1) [A. H]:
# Ifndef header_a
# Define header_a
Extern int B; // declare the int variable
# Endif
(2) [A. cpp]:
# Include "A. H"
(3) [main. cpp]:
# Include "A. H"
# Include "A. H" // yes, because there is a header file guard
Int B = 3; // you can define B in A. cpp, but only one. cpp can be selected in two. cpp and B can be defined.
Main (){...}