Objective
Today, when sorting out your code, consider that the code I wrote from one to the end is in a CPP file. So, you want to separate the modules in your code so that you can read and manage them better.
Problems encountered
My approach is to:
- Macro definitions, struct definitions, function declarations, and global variable definitions are placed in a head.h header file
- The definition of the function is placed in the Head.cpp
- The main function is placed in the main.cpp
However, the error indicates that the XXX variable is defined in the *.obj file.
Why the problem occurred
Why does this happen?
- First, the compilation of individual files is independent. Compile to Main.obj in Head.cpp compilation to Head.obj,main.cpp. This process has no error, which means that the compilation process is not a problem.
- Next is the link to obj. When linking Main.obj and Head.obj, the compiler discovers that Head.obj allocates memory space for these global variables, and that the global variables are allocated memory space in Main.obj.
- There are two different memory addresses for the same variable. The compiler then gave an error.
It's not a way.
Add static to the global variables of the header file inside the head.h. Compilation can be passed, but there are other problems inadvertently.
Static only extends the lifetime of the variable and also restricts the variable to the current file. The reason why it can be used in main.cpp is because it replicates a variable with the same variable name at compile time to main.cpp. Then the change of the "global variable" inside the main.cpp does not change the value of the global variable inside the original head.h.
Although the compilation passed, but the program is wrong.
The real Solution
- Place the global variable definition in the Head.cpp file.
- The declaration of the global variable is stored in Head.h, and is modified before each declaration
extern
.
My personal thoughts.
I think one of the things you can do to be able to separate global variables is:
- The global variable definition is still placed in the head.cpp.
- Create a new global.h header file that holds the declaration of the global variable and modifies it before each declaration
extern
.
- The global.h header file is included when other files need to use global variables.
Knot words
The problem arises because the C language has not been used for too long. Moreover, in the use of C or C + + language, often because the experiment and the class set up need to write the code is not too much, so developed a habit, a main.cpp write to the end. When it comes to separating its own module code, it is not possible to find out that a defined global variable causes a compile link error. So write down this article beware of yourself! The article may have the wrong place, hoped that everybody can correct!
Article from Kwongtai ' blog, reproduced please indicate the source
Solve the problem of repeated definition of global variables in the C + + language