Qt: Hassling with multiple "multiple definition..." linker complaints

Posted on 2004-08-22
Last Modified: 2008-01-09
Hi friends,

I just don't get what's wrong here - see "make" output below. I've googled for this question and found lots of others having similar trouble, and I believe I followed all the advice: Separate the class definition in a header file, protect headers with #ifdef, place the Q_OBJECT macro up there too, have qmake create a fresh project file and makefile, learn about moc, and and and. However, must have missed something at some point. Can anybody point me out where the problem is? I've been programming in a handful languages, but just getting my toes wet in C++ and Qt, so it's probably a newbie question. Attached are links to all sources plus project file and makefile.

Thanks in advance,

- make outputs this:

g++ -c -pipe -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -fPIC -Wall -W -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -fPIC  -DQT_NO_DEBUG -DQT_SHARED -DQT_TABLET_SUPPORT -I/usr/lib/qt3/mkspecs/default -I. -I. -I/usr/include -I/usr/lib/qt3/include -o fxsession.o fxsession.cpp
g++ -c -pipe -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -fPIC -Wall -W -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -fPIC  -DQT_NO_DEBUG -DQT_SHARED -DQT_TABLET_SUPPORT -I/usr/lib/qt3/mkspecs/default -I. -I. -I/usr/include -I/usr/lib/qt3/include -o main.o main.cpp
/usr/lib/qt3/bin/moc fxsession.h -o moc_fxsession.cpp
g++ -c -pipe -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -fPIC -Wall -W -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -fPIC  -DQT_NO_DEBUG -DQT_SHARED -DQT_TABLET_SUPPORT -I/usr/lib/qt3/mkspecs/default -I. -I. -I/usr/include -I/usr/lib/qt3/include -o moc_fxsession.o moc_fxsession.cpp
g++  -o fx fxsession.o main.o moc_fxsession.o   -L/usr/lib/ -L/usr/lib/qt3/lib/ -L/usr/X11R6/lib/ -lqt -lXext -lX11 -lm
main.o(.text+0x0): In function `FXSession::setPassword(QString)':
: multiple definition of `FXSession::setPassword(QString)'
fxsession.o(.text+0xc80): first defined here
main.o(.text+0x30): In function `FXSession::setUser(QString)':
: multiple definition of `FXSession::setUser(QString)'
fxsession.o(.text+0xcb0): first defined here
main.o(.text+0x60): In function `FXSession::start()':
: multiple definition of `FXSession::start()'
fxsession.o(.text+0x0): first defined here
main.o(.text+0x720): In function `FXSession::ready(bool)':
: multiple definition of `FXSession::ready(bool)'
fxsession.o(.text+0x6c0): first defined here
main.o(.text+0x8f0): In function `FXSession::~FXSession [in-charge deleting]()':
: multiple definition of `FXSession::~FXSession [in-charge deleting]()'
fxsession.o(.text+0x890): first defined here
main.o(.text+0xa40): In function `FXSession::~FXSession [in-charge]()':
: multiple definition of `FXSession::~FXSession [in-charge]()'
fxsession.o(.text+0x9e0): first defined here
main.o(.text+0xb90): In function `FXSession::~FXSession [not-in-charge]()':
: multiple definition of `FXSession::~FXSession [not-in-charge]()'
fxsession.o(.text+0xb30): first defined here
main.o(.text+0xce0): In function `FXSession::FXSession[in-charge](QString, QString)':
: multiple definition of `FXSession::FXSession[in-charge](QString, QString)'
fxsession.o(.text+0xce0): first defined here
main.o(.text+0x1750): In function `FXSession::FXSession[not-in-charge](QString, QString)':
: multiple definition of `FXSession::FXSession[not-in-charge](QString, QString)'
fxsession.o(.text+0xf00): first defined here
collect2: ld returned 1 exit status
make: *** [fx] Error 1

Here are the source files: (generated)

Here are the project file and makefile as generated by qmake:
Question by:k-f-a
  • 3
LVL 19

Expert Comment

ID: 11863955
Ok, after having a quick look at the source I can see one thing instantly that is wrong:

// In fxsession.cpp

#include "fxsession.h"
#include <qapplication.h>
#include <qstring.h>
#include <qtextstream.h>
#include <qregexp.h>
#include <qhttp.h>

// in fxsession.h

#include <qapplication.h>
#include <qstring.h>
#include <qtextstream.h>
#include <qregexp.h>
#include <qhttp.h>

In other words all of the <> includes are including twice, firstly in fxsession.cpp and then again in fxsession.h.  

Change the includes of fxsession.cpp to:

#include "fxsession.h"

and that should help.  Cannot do much else since I have not got the <> includes...


Author Comment

ID: 11865161
Thank you mrwad99, I tried what you suggested, but there is no change in the result. The qt headers are not the troublemakers anyway, they're properly encapsuled and you can include them as often as you want, they get compiled only once. It is my own fxsession class causing the headache, and I don't understand what's happening because its definition is enclosed with an #ifndef as well.

Accepted Solution

havman56 earned 50 total points
ID: 11870872

1. remove #include "fxsession.cpp" in main
2.  see this line in ur make file
moc_fxsession.o moc_fxsession.cpp
g++  -o fx fxsession.o main.o moc_fxsession.o   -L/usr/lib/ -L/usr/lib/qt3/lib/ -L/usr/X11R6/lib/ -lqt -lXext -lX11 -lm

1. here fxsession .h --> fxsession.o
2. fxsession.h--->moc_fxsession.cpp
3.moc_fxsession.cpp ->moc_fxsession.o

so u have 2 copies of same fxsession so " fxsession.o main.o moc_fxsession.o " check it !

3. main.o(.text+0x0): In function `FXSession::setPassword(QString)':
: multiple definition of `FXSession::setPassword(QString)'
fxsession.o(.text+0xc80): first defined here

see this piece of code  u can see 2 copies what ever i said before.

4. Open your .map file which wil give the memory map of your files . From there
u can identify .

5. I belive some where ur make script u  are including twice

6.If u see the spacing between them is 0xC80 so some object file is there inthat memory image.

7. Remove everrything include it in .h and use it here
#include <qapplication.h>
#include <qstring.h>
#include <qtextstream.h>
#include <qregexp.h>
#include <qhttp.h>

8. If u happen to get same problem then declare it as extern functions  which can fool the compiler and may work in linker time.



Author Comment

ID: 11874019
Oh dear, should have seen it before. Replacing #include "fxsession.cpp" with #include "fxsession.h" in main.cpp was all it took to compile.
Thanks :-)

Author Comment

ID: 11874168
PS: Had a quick look back up the page... mrwad99, I didn't do you justice. You pointed it out before, but I overlooked it. Sorry about that.

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question