简体   繁体   中英

How to find the multiple definitions of a function

I wrote a findDialog which finds the text searched. When I give make command,it returns

g++ -Wl,-O1 -o findDialog FindDialog.o main.o moc_FindDialog.o    -L/usr/lib -lQtGui -lQtCore -lpthread 
moc_FindDialog.o: In function `FindDialog::findClicked()':
moc_FindDialog.cpp:(.text+0x20): multiple definition of `FindDialog::findClicked()'
FindDialog.o:FindDialog.cpp:(.text+0x30): first defined here
moc_FindDialog.o: In function `FindDialog::enableFindButton(QString const&)':
moc_FindDialog.cpp:(.text+0x50): multiple definition of `FindDialog::enableFindButton(QString const&)'
FindDialog.o:FindDialog.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [findDialog] Error 1

I have searched the problem for hours, but I can not understand what the problem stems from. What can cause multiple definition of error?

It usually happens when method definition is included in multiple translation units, also called as object files. Later, when linker is combining those object files, it finds out that there are multiple definitions of the same method, and complains because it doesn't know which one to use. Here is a simple example of how to introduce this error:

Have header file header.hpp with both method declaration and its definition:

class foo {
public:
  void bar ();
};

void foo::bar ()
{
}

And have two source files source1.cpp and source2.cpp both including that file:

source1.cpp :

#include "header1.hpp"
int example1()
{
  foo f;
  f.bar ();
}

... and source2.cpp :

#include "header1.hpp"
int main ()
{
  foo f;
  f.bar ();
  f.bar ();
}

Then, compile two files separately and link them together. For example:

g++ -c source1.cpp source1.o
g++ -c source2.cpp source2.o
g++ -o a.out source1.o source2.o

That will give you a linker error you have described in your question, because method foo::bar appears twice, in both source1 and source2 objects. Linker doesn't know which one to use.

There are two common solutions to this problem:

Solution #1 - Have that method inlined.

By declared that method with inline keyword, compiler will either inline the whole method or, if it decides not to, it will generate anonymous method (same method but with some unique name for a given object file), so there will be no conflicts in object files. For example:

class foo {
public:
  void bar ();
};

inline void foo::bar ()
{
}

Solution #2 - Have definition (implementation) of that method in another source file, so that it appears only once in the whole program. For example:

header1.hpp :

class foo {
public:
  void bar ();
};

header1.cpp :

#include "header1.hpp"
void foo::bar ()
{
}

To decide whether to inline or not, you have to know (or at least make a guess) whether calling this function is more expensive than having this code duplicated / inlined all over the program. Inlined code usually makes your program bigger and increases compilation time. But it doesn't necessarily make it faster. Plus, having definition in source file will not result in re-compilation of all source files using that function, but only re-compilation of the source file that has definition, and then re-linking. Many programmers are going crazy about C++ inlining without really understanding how it affects the program. I'd recommend going with the definition in a source file and making it inline only if calling that function becomes a performance bottleneck and otherwise having it inlined will fix it.

Hope it helps. Happy coding!

Writing function definitions in header files, and then #include ing these header files in multiple .cpp s in your project. Contrary to popular misconception, header guards do not protect you from this.

Write only the following in headers:

  • Class definitions
    • May include inline definitions for member functions
  • Non-member variable declarations
  • Non-member function declarations
  • Template/inline function definitions

Put everything else in "source files".

Vlad Lazarenko's answer is good.

I just add another possibility that I met when using QT/C++:

When you declare a slot, you don't need to write a definition (implementation), otherwise, you will get an error: multiple definition

Judging from file names in the error messages, you have some mock test fixtures as part of TDD and some real code. The linker did report where the trouble is fairly clearly - I've reformatted the information here for extra clarity by removing part of the information:

moc_FindDialog.cpp: multiple definition of `FindDialog::findClicked()'
FindDialog.cpp:     first defined here

moc_FindDialog.cpp: multiple definition of `FindDialog::enableFindButton(QString const&)'
FindDialog.cpp:     first defined here

This clearly states that the linker first encountered each of the function definitions in moc_FindDialog.cpp , and then encountered the second definition in FindDialog.cpp .

I think you need to look hard at the mock test fixture in moc_FindDialog.cpp and determine why those two functions are replicated. Or, maybe you are not supposed to link both the mock functions and the real functions in a single program - the two source files are simply not intended to be linked into a single program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM