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.hppwith both method declaration and its definition:And have two source files source1.cpp and source2.cpp both including that file:
source1.cpp:… and
source2.cpp:Then, compile two files separately and link them together. For example:
That will give you a linker error you have described in your question, because method
foo::barappears 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
inlinekeyword, 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: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:header1.cpp: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!