LNK2005 errors have basically been encountered during the compilation of C ++ programs. The following is a detailed analysis of this problem:
First, the preprocessing phase:
This process mainly targets # include and # define. The specific process is as follows:
# Include usually appears in the cpp file to include a header file. After preprocessing, all the # include commands will be replaced with the details in the header file, if the header file contains another header file, use the same method for Recursive processing. The same is true for # define. replace macro-Defined Characters with a simple replacement. Therefore, the entire preprocessing process is a simple replacement.
Second, the pre-compilation process
This process is mainly used to compile the pre-processed cpp file called the obj file, that is, the target file (Note: Here it is only for the cpp file, the first file is not processed, therefore, when defining global variables in a multi-File project, we must remember to define the global variables in the cpp file, because if it is defined in the header file, if we want to use this variable, the already define error will easily occur ). It mainly generates binary files through the compiler and assembler to facilitate link processing.
For this, I will give an instance and its explanation:
During the link process, the variable declared with extern is clearly known as an external variable. For this reason, the connector will look for this variable in the external obj file, if the definition of this variable is included in the cpp compiled by this obj file, this is not a declaration, but a definition. Therefore, the source file cpp compiled by our obj file must not contain the header file or cpp file with the variable definition. Otherwise, it is re-defined, that is to say, some redefinition is an error that will be identified during the link, so it is best to be in. use extern to declare in the H file, and then define it in any cpp file. You only need to include this header file in the cpp file that you want to use this variable.
Third, the link Process
If you do not have enough vigilance in writing a program, a link error may easily occur in this process. First, this process mainly generates a large number of obj files.