Solved

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

Posted on 2004-08-22
5
1,177 Views
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,
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
Comment
Question by:k-f-a
  • 3
5 Comments
 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
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
 

Author Comment

by:k-f-a
Comment Utility
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
 
LVL 4

Accepted Solution

by:
havman56 earned 50 total points
Comment Utility

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
 

Author Comment

by:k-f-a
Comment Utility
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
 

Author Comment

by:k-f-a
Comment Utility
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now