How to find the multiple definitions of a function
14,551
Solution 1
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".
Solution 2
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
Author by
stedkka
Updated on June 09, 2022Comments
-
stedkka almost 2 years
I wrote a findDialog which finds the text searched. When I give
make
command,it returnsg++ -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? -
stedkka almost 13 yearsYes, you are right about header guards which are widely advised to use to solve the problem . However they didn't help.
-
Lightness Races in Orbit almost 13 years@stedkka: Header guards solve a subtly different problem (which is, speaking very generally, multiple declaration rather than definition).