Today I learned a bit about the use of global variables in the project and considerations, here to share with you:
1. Usage
Common uses in engineering are:
Declare global variables with extern in the "gv.h" file, as
extern int GV
Define global variables in the "gv.cpp" file, such as
int GV;
Use and invoke global variables (including "gv.h" files) in other files, such as
Gv=1
global variables are not directly defined in the "gv.h" file, but are defined in the ". cpp" file because:
The definition of the variable cannot be placed in the header file!!!
In general, only the declaration of variables is placed in the head file,
Because the header file is to be contained by another file (that is, # include),
If you put the definition to the header file, you can not avoid defining variables more than once, C + + does not allow multiple definitions of variables, a program in the definition of the specified variable only once, the declaration can be countless times.
Ps:
There are three exceptions, and the definition of the entity in the three can also be placed in the header file.
1. The definition of a const variable that the value is known at compile time can be placed in the header file
such as: const int num (10);
2. Class definitions can be placed in the header file
3.inline function
These three entities can be defined in more than one source file, as long as the definitions in each source file are the same.
2. precautions
1th, array extern usage
An array is defined in a source file: Char a[6];
In another file, it is declared with the following statement: extern char *a;
Is that OK?
Answers and Analysis:
1) no , the program will tell you illegal access when it runs. The reason is that pointers to type T are not equivalent to arrays of type T. extern Char *a declares a pointer variable instead of a character array, and therefore differs from the actual definition, resulting in illegal access by the runtime. The declaration should be changed to extern char a[].
2) example analysis is as follows, if a[] = "ABCD", then the external variable a=0x12345678 (the starting address of the array), and *a is redefining a pointer variable, the address of a may be 0x87654321, the direct use of *a is wrong.
3) This prompts us to use the extern time to strictly correspond to the format of the declaration, in the actual programming, such errors are not uncommon.
4) extern is often used in variable declarations, where you declare a global variable in the *.c file, and if the global variable is to be referenced, it is placed in *.h and declared with extern.
2nd, the different nature of the process of compiling links (to learn the principles of compiling AH!) )
Declaring an external variable
Modern compilers generally take the form of compilation by file, so at compile time, the global variables defined in each file are opaque to each other. That is, at compile time, the visible field of the global variable is limited to the inside of the file.
Here's a simple example:
Create a project that contains A.cpp and B.cpp two simple C + + source files:
A.cppint I;int Main () {}//b.cppint i;
these two files are extremely simple, in A.cpp we define a global variable I, in B we also define a global variable i.
We compile the A and B separately, can be compiled normally, but when the link, there is an error, the error is as follows:
Linking ... b.obj:errorlnk2005: "Inti" ([email protected]@3ha) alreadydefinedina.objdebug/a.exe:fatalerrorlnk1169:o Neormoremultiplydefinedsymbolsfounderrorexecutinglink.exe.a.exe-2error (s), 0warning (s)
This means that during the compilation phase, the global variables defined in each file are opaque to each other, and when compiling a does not perceive that I is also defined in B, similarly, when compiling B, I is not aware of a.
However, at the link stage, the contents of each file are "integrated", so if the names of the global variables defined in some files are the same, then an error occurs at this point, which is the duplicate definition error indicated above.
Therefore, the global variable names defined in each file are not identical.
During the link phase, the contents of the individual files (which are actually compiled from the obj file) are merged together, and thus, the global variables defined in a file are expanded to the entire program after the link is complete.
As a result, the global variables defined in a file can be used anywhere in the program, for example, if a global variable is defined in a file, then the variable should be available in the B file. Modify our program to verify that:
A.cppint Main () { i=100;//attempted to use the global variable defined in B}//b.cppint i;
The compilation results are as follows:
Compiling ... a.cppc:\documentsandsettings\wangjian\ Desktop \tryextern\a.cpp (5): errorC2065: ' I ': Undeclaredidentifiererrorexecutingcl.exe.a.obj-1error (s), 0warning (s)
compilation error.
This error is expected because the visibility of the global variables defined in the file extends to the entire program after the link is complete, and in the compilation phase, their visibility is still limited to their own files.
The compiler is not looking long enough, and the compiler is not aware that a variable symbol, although not defined in this file, may be defined in other files.
Although the compiler is not visionary enough, we can give it hints to help it solve the problem above. This is the role of extern.
The principle of extern is simply to tell the compiler: "You are compiling a file that has an identifier that is not defined in this file, but it is a global variable defined in another file, you have to release it!" ”
We add the extern keyword to the above error program:
A.cppextern int I;int Main () { i=100;//attempted to use the global variable}//b.cppinti defined in B;
smooth through compile, link.
3rd, extern usage of two-dimensional arrays
Problem:
I have defined a global two-dimensional array, such as Double gg[8][8];
I want to call him in another file, how do I use extern to declare it?
Answer:
extern double gg[][8]; A two-dimensional array declaration must be followed by a number of columns otherwise the compiler does not know how the two-dimensional array is organized
C + + Global variables Engineering usage