Make found the problem as follows:
I'm putting
pthread_mutex_t Vt_mutex;
This sentence is written in init.h. Then many files contain this init.h.
in general, when you define a variable declaration in the. h file, in other files, as long as you include this. h file, the compile will be independently interpreted by the compiler, and then each of the. c file will generate a separate identifier and symbol table, so the above code in a separate compile without error, syntax is legal. However, at the end of the compiler link, the project will be all the symbols together, because the file has duplicate variables, so there will be repeated definition of the error, the system is prompted you "multiple definition of ' xxx '". Further explanation, we can imagine compiling every C source file, the equivalent of a pipe surrounded by longitudinal flow, the two do not interfere. When the link is two separate water pipes flow horizontally, all the duplicate elements appear. So when a link is made, a duplicate-defined identifier appears. Duplicate-defined identifiers are just variables here, and functions are not. Because the function is really only defined once in. c, multiple declarations are fine, and the variable does appear with two definitions. Two repeated variable definition linker does not know that the address is the memory of the variable, so the error. How to solve this problem?
In fact, you just need to move the global variable definition from the. h file to the. c file, and then use extern to make an external declaration in the. h file. Declare the variable in the. c file, and then add extern to all of the variable declarations in header file. h, and note that the variable is not initialized in the. h file. then other. c files that need to use global variables contain. h files. The compiler will generate the target file for. C, and the linker will link to this. c file if the. c file uses a global variable when it is linked. Other files need to use this global variable is the same way, in fact, only one, is to make the variable in memory unique.
Solution:
The pthread_mutex_t Vt_mutex; Written in init.c, init.c include Init.h
Write an extern pthread_mutex_t Vt_mutex inside the init.h;