Multiple definition...

Source: Internet
Author: User

The solution to multiple definition of is summarized as follows:
Cause:
When multiple files contain the same header file, and your. h file does not contain Conditional compilation
# Ifndef test_h
# Define test_h
# Endif
Each file is generated to generate an independent identifier. When the compiler is connected, all the symbols in the project are integrated. Because there are duplicate variables in the file, a duplicate definition error occurs.

Method 1:
It is a habit to add Conditional compilation to each header file to avoid multiple interpretations when the file is referenced multiple times. This method will solve most of the low-level problems.

Method 2:
When method 1 is invalid, all global variables can be put into a header file global. in H (the name is random, but Conditional compilation is required), each variable is prefixed with extern and declared that these variables will be defined in other files. Create a. C or. cpp file corresponding to the header file name, such as global. C. Declare all global variables in it. Example: void (* handl_display )();
Then, let the file that involves global variables include "Global. H". In this way, a global. O will be generated for global. c compilation, and then delivered to the. otrace of other files into executable files.

Method 3:
The lazy method adds static before all global variables to declare static variables. It can also solve the problem.
All methods come from the Internet, O (∩ _ ∩) O Haha ~

Thanks to all the guys who provide the methods ~

ArticleSource: http://general.blog.51cto.com/927298/235077

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.