• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1227
  • Last Modified:

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

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,
Klaus

- 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:
http://puma.dyndns.biz/~klaus/fx/main.cpp
http://puma.dyndns.biz/~klaus/fx/fxsettings.h
http://puma.dyndns.biz/~klaus/fx/fxsession.h
http://puma.dyndns.biz/~klaus/fx/fxsession.cpp
http://puma.dyndns.biz/~klaus/fx/moc_fxsession.cpp (generated)

Here are the project file and makefile as generated by qmake:
http://puma.dyndns.biz/~klaus/fx/fx.pro
http://puma.dyndns.biz/~klaus/fx/Makefile
0
k-f-a
Asked:
k-f-a
  • 3
1 Solution
 
mrwad99Commented:
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...

HTH
0
 
k-f-aAuthor Commented:
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.
0
 
havman56Commented:

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

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

9.
 

 
0
 
k-f-aAuthor Commented:
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 :-)
0
 
k-f-aAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now