Problem solving: multiple definition of XXX

Source: Internet
Author: User

When compiling the program, there is a problem, take some time to record:

After creating a class in Qt, it is generally necessary to declare the variables and functions in the. h file, and then define the functions in the corresponding. cpp file, which is not a problem in the usual use, and today when using QT, there are many multiple definitions in each. cpp source file when compiling. The error of XXX.

The search for some information on the Internet is finally a way to solve multiple definition of:

One of the problem solving methods:

According to the online explanation, multiple definition of reason is because variables and functions are defined repeatedly when the global.h is included multiple times. The problem is that after examining the program, it is found that there is no duplicate defined variables and functions, in a forum comment to try a simple rough method ... Take the QT project example and find the compile-time O file in the project's debug folder, such as:

C and C + + compile. c,cpp files, each file generates an. o file, and then all the. o files are linked to the final execution program, and the executable file cannot be generated if there is a problem with the O file. Delete the existing O file, recompile and generate a new O file, resulting in a successful execution of the program without other problems . (This is where I belong ...) You can try it if there is no obvious error in the confirmation code)

Two Problem solving methods:

When multiple files contain the same header file, and your. h file does not add conditional compilation.

When multiple files contain the same header file, and the header file is not added to the conditional compilation, it is interpreted independently and generates a separate identifier for each file. 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.

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. The conditional compilation was generated by default when the Qt class was created:

#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.

Therefore, it is 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.

Problem Solving method Three:

When the above method is invalid, you can put all the global variables into a header file Global.h (the name random, 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.

Another workaround mentioned in an article: http://blog.csdn.net/wu070815/article/details/8781762:

Add static to the variable before it is declared as a static variable. While this approach solves the problem of multiple definition, it can cause other problems. The problem is as follows: three files, a. hA. CB. C;In a. Cand b. CAll include A.. h。 In B. CCall A In. CThe function in the A. h, but in fact B. CThe variable in is still not assigned. Problem Analysis: The meaning of static is to force that variable to be visible only in a file. Assume that you define static in a header filex; And this header file is individually. Cand b. CContains;The essence is in a. Cand b. CWill define a name calledxThe variables, twoxIt doesn't matter. In a. CChanges inxThat he would not cause B. CIn thexChange.

Problem solving: multiple definition of XXX

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.