Solved

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

Posted on 2004-08-22
5
1,183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 19

Expert Comment

by:mrwad99
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...

HTH
0
 

Author Comment

by:k-f-a
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.
0
 
LVL 4

Accepted Solution

by:
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

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
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 :-)
0
 

Author Comment

by:k-f-a
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

734 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