Basics:
A. cpp corresponds to an OBJ, and A. obj is a compilation unit. Multiple compilation units are linked through the linker to form an EXE, which is a program. If a CPP needs a function defined by another CPP, you only need to write the declaration of this function in the CPP, note: If a function or variable with the same name is in different OBJ during the link process, an error will be reported when the program runs. (In fact, during compilation, a redefinition error will be prompted .) In the actual debugging process, it is found that variable redefinition can be detected immediately during compilation, while function redefinition is different, if you implement the definition of this function in one CPP, and then run it successfully, and then implement the same function definition in another CPP function, the program will not report an error, after rebuild all, an error is reported. I personally think it is the cause of Compiler optimization.
Methods to avoid this problem: extern and static; extern are external links, that is, in addition to this unit, external units
Static is an internal link and belongs to its own unit.
TIPS: do not define global variables in. H files. If there are two. cpp files and above that contain this. h file, variable redefinition will appear.
To use a global function, you can define the variable in a CPP file. extern in the CPP file (No header file is required )(. H files will be expanded in the CPP file during compilation ).
Static keywords can only be seen in the current compilation unit, but not externally.
Remember: In C ++, the variable declaration is valid only when the extern keyword is used. In other cases, it is defined. When the extern keyword is used, the variable is assigned an initial value, the Declaration is defined, and the extern is ignored by the compiler (verified by the compiler ). C ++ variables are global by default and visible to all source files. If the static keyword is added, it is considered a local variable and invisible to the outside.
1. Do not put the definition in. H, which may lead to repeated definitions.
2. Try to define the variable as static within the local range unless global.
Const and typedef are exceptions. Const and typedef are local by default and do not need to use static. the use of the const variable in the H file does not produce redefinition, And the compiler references this for each. h's source file applies for a local definition of the variable, just as the variable is defined in the original file, but at this time it is independent from other source files that use the variable and there is no connection with each other.
3. for static keywords, if a variable defined in a function or a member variable defined in a class is modified, it is called a static variable or a local variable when it is modified.
For functions, the default value is extern. Generally, all functions are "external" functions,
Therefore, you do not need to add "extern" When declaring a function ".
If "static" is added when a function is declared in a file, this function is only visible in this file.
<> H file Function
1. Convenient development: contains the common constants, structures, types, functions, and variable declarations required by some files,
2. Interfaces: for a software package, an interface can be provided to the outside world (for example, stdio. h)
<> What should I have in the H file?
Constant, structure, type definition, function, variable Declaration
<> There should be no hfile
Variable definition, Function Definition
<> Extern
For variables that require extern,
For a function, the default state of the function is extern. If a function is to be visible only in the file, add static
<> Include Inclusion Problems
Although declarations and type definitions can be repeated, Conditional compilation # ifndef _ filename_h, # DEFINE _ filename_h, # endif is recommended.
<> The H file should be included there.
Where necessary. For example, if the H file providing an interface is only required by the 1.C file, it is included in the 1.C file.
The purpose of Conditional compilation is to prevent repeated inclusion and compilation of header files, which may reduce the compilation speed, but is not an error !?