[Problem resolution] multiple definition of ' * * * '

Source: Internet
Author: User

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;





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.