C ++ # include. h extern correlation and description,
Both functions and variables are written in either voice or definition. The purpose of the Declaration is to tell other code the name of the variable, and the definition has a function more than the Declaration. In addition to the name, the variable will be allocated with memory.
For a function, adding or not adding extern does not matter, because a function body must be defined, and the addition of extern will also be ignored. If there is no function body, it is the declaration,
Int add (int a, int B );
Equivalent to extern int add (int a, int B );
If it is a variable
Int a; // defines a variable.
Extern int a; // declares a variable.
Variables can have any declaration, but only one definition is allowed. global variables are defined in multiple files. Only one file does not contain extern, And the rest must contain extern to indicate declaration.
Note: In the. h file, it is generally used to declare some variables. Not definitions. All definitions and implementations are in the. c or. cpp file.
Therefore, pay special attention
Note the following when defining a global variable, which is a global variable:
The declaration of global variables is generally written in. in the H file, add extern, because the definition variable is not added, so it is not correct. in this case, # include this. all files in the H file can access this global variable. however, this variable must be defined in. c or. cpp file. there can only be one definition of this variable.