This paper summarizes the method of solving multiple definition of

Source: Internet
Author: User

Cause of the problem:
When multiple files contain the same header file, and your. H does not add conditional compilation
#ifndef Test_h
#define Test_h
#endif
will be interpreted independently, then generate separate identifiers for each file generated. When the compiler joins, all the symbols in the project are combined, because there are duplicate variables in the file, so there is a repeating definition of the error.

Method 1:
It should be customary to add conditional compilation to each header file to avoid being interpreted many times when the file is referenced multiple times. This approach will solve most of the low-level problems.

Method 2:
When method 1 is invalid, you can put all the global variables into a header file Global.h (the name is arbitrary, but to add conditional compilation), each variable is preceded by extern, declare that these variables will be defined in other files. Then create a. C or. cpp file that corresponds to the header file name, such as GLOBAL.C. Declare all the global variables inside. For example: void (*handl_display) ();
Then, let the file that involves the global variable include "global.h". When compiling this way, a GLOBAL.O is generated for GLOBAL.C compilation, and then the. O links to other files are delivered to the executable file.

Method 3:
After understanding the use of the static definition function, you can add static to all global variables before declaring them as static variables. can also solve the problem.

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.