In order to allow the program to be split into several logical parts, the C + + language supports a separate compilation mechanism, allowing the program to be split into several files, each of which can be compiled independently.
To support split compilation, C + + separates declarations and definitions. A declaration makes a name known to the program, and a file must contain a declaration of that name if it wants to use a name defined elsewhere. The definition is responsible for creating the entity associated with the name.
Variable declarations specify the type and name of the variable, define additional storage space for the request, and may assign an initial value to the variable.
To declare a variable instead of defining it, add the keyword extern before the variable name, and do not show the Initialize variable:
extern int i; // declaring I rather than defining int J; // declare and define
Any initialization declaration that contains a display is defined as:
extern Double 3.1416; // definition
Inside the function body, an error is raised if an attempt is made to initialize a variable that is marked by the extern keyword.
Variables can only be defined once, but may be declared more than once.
If you want to use the same variable in multiple files, you must declare and define the detach. At this point, the definition of the variable must appear and appear in only one file, while the other file used by the variable must be declared and never be defined repeatedly.
C + + Prime: The relationship between variable declarations and definitions