Qt Forum - programming with C++ and Qt
Register Calendar Members List Team Members Search Frequently Asked Questions Go to the Main Page

Qt Forum - programming with C++ and Qt » QtForum.org » Qt » Qt Programming » Multiple Declaration + Endless Linker Errors » Hello Guest [Login|Register]
Last Post | First Unread Post Print Page | Recommend to a Friend | Add Thread to Favorites
Post New Thread Post Reply
Go to the bottom of this page Multiple Declaration + Endless Linker Errors
Author
Post « Previous Thread | Next Thread »
rohanprabhu rohanprabhu is a male
Assistant


Registration Date: 11.03.2008
Posts: 4

Level: 6 [?]
Experience: 266
Next Level: 282

16 points of experience needed for next level

Multiple Declaration + Endless Linker Errors Reply to this Post Post Reply with Quote Edit/Delete Posts Report Post to a Moderator       Go to the top of this page

I am entirely new to programming with Qt, though I'm quite familiar with C++. I am using Qt 4.2.3 under the GPL version. I configured Dev-C++ to use Qt using this guide: http://darkhack.googlepages.com/qttutorial

I made a new Dev-C++ project and selected 'Qt', as the settings required are pre-entered in the template file. I am reading a book from which I was able to run a 'Hello World' app easily. The next chapter included creation of a dialog. The files in the project were:

finddialog.h
finddialog.cpp
main.cpp

{Please click on the file names to view the source of each file}. When I tried to compile it though, I got the following error:

quote:

multiple definition of `FindDialog::FindDialog(QWidget*)'
first defined here
multiple definition of `FindDialog::FindDialog(QWidget*)'
first defined here


at this line:

code:
1:
2:
3:
FindDialog::FindDialog(QWidget *parent) : QDialog(parent) {


I get the 'multiple definition' error two times for each of the member function that in the FindDialog class including the constructor, as mentioned above.

When I tried to run this in Code::Blocks, I still got the same error and it gives a 'multiple declaration' error and a 'first defined here' on the same line no. and even Code::Blocks displays it 2 times.

In addition to that, I get an endless list of linker errors like:

quote:

[Linker error] undefined reference to `FindDialog::staticMetaObject'
[Linker error] undefined reference to `vtable for FindDialog'
[Linker error] undefined reference to `vtable for FindDialog'
.....


I am unable to do anything further than this and it is confusing me more and more. Please help.

EDIT:

I re-read the book, and it asks me to include 'finddialog.h' in the main file and not 'finddialog.cpp'. One doing so, I am able to get rid of the 'mutliple definition errors', but the Linker errors are still there. Also, could anyone tell me as to why the book tells me to include 'finddialog.h' when 'finddialog.cpp' already includes it and 'finddialog.cpp' defines the member functions. Also, why does the compiler think that multiple definitions occur even if the cpp file is included only once? Thanks a lot.

Thanks,
rohanprabhu
11.03.2008 11:27 rohanprabhu is offline Send an Email to rohanprabhu Search for Posts by rohanprabhu Add rohanprabhu to your Buddy List
T.M.F. T.M.F. is a male
Chief Software Engineer


Registration Date: 21.11.2006
Posts: 140

Level: 24 [?]
Experience: 75,857
Next Level: 79,247

3,390 points of experience needed for next level

Reply to this Post Post Reply with Quote Edit/Delete Posts Report Post to a Moderator       Go to the top of this page

You must include "finddialog.h" in main.cpp because it contains definition of the class FindDialog. Without it main.cpp knows nothing about this class. It must be included in finddialog.cpp for the same reasons.
Ok, how is looking your project file (.pro)? Did you generate makefile automatically using qmake or it is generated by codeblocks? And last think, classes which contain Q_OBJECT macro must be preprocessed by moc (meta-object compiler). That's why linker cannot find some entries in class vtable.
Try to look into Qt examples directory.
12.03.2008 03:52 T.M.F. is offline Send an Email to T.M.F. Homepage of T.M.F. Search for Posts by T.M.F. Add T.M.F. to your Buddy List
rohanprabhu rohanprabhu is a male
Assistant


Registration Date: 11.03.2008
Posts: 4

Level: 6 [?]
Experience: 266
Next Level: 282

16 points of experience needed for next level

Thread Starter Thread Started by rohanprabhu
Reply to this Post Post Reply with Quote Edit/Delete Posts Report Post to a Moderator       Go to the top of this page

first of all.. thanks a lot for replying.

quote:
Originally posted by T.M.F.
Ok, how is looking your project file (.pro)? Did you generate makefile automatically using qmake or it is generated by codeblocks?


Since i'm making a project, Code::Blocks generates the Makefile. But, i tried using qmake as well. But this didn't help.

quote:
And last think, classes which contain Q_OBJECT macro must be preprocessed by moc (meta-object compiler). That's why linker cannot find some entries in class vtable.
Try to look into Qt examples directory.


In the qt documentation, it is written:

quote:

if you use qmake to create your makefiles, build rules will be included that call the moc when required, so you will not need to use the moc directly


Once, i've generated a makefile using qmake, how do I go ahead to compiling it. Once I run qmake, I get these files: Makefile, Makefile.debug, Makefile.release [first i get a .pro file, and after running qmake on the .pro file, i get the files i mentioned].

I have no idea how to use them. Do I use it these files with make? I tried using make on the Makefile, and this is the output I got:

quote:

make: *** No rule to make target `qt-cpp.pro', needed by `Makefile'. Stop.


Thanks,
rohanprabhu

This post has been edited 1 time(s), it was last edited by rohanprabhu: 12.03.2008 05:25.

12.03.2008 05:20 rohanprabhu is offline Send an Email to rohanprabhu Search for Posts by rohanprabhu Add rohanprabhu to your Buddy List
rohanprabhu rohanprabhu is a male
Assistant


Registration Date: 11.03.2008
Posts: 4

Level: 6 [?]
Experience: 266
Next Level: 282

16 points of experience needed for next level

Thread Starter Thread Started by rohanprabhu
Reply to this Post Post Reply with Quote Edit/Delete Posts Report Post to a Moderator       Go to the top of this page

I was able to make it run!!! but the problem is that the process is hugely manual and I need to somehow ease it out.. i.e. automate it.

This is how i got it <barely> working big grin :

First, I ran moc on finddialog.h in the command line:

code:
1:
moc finddialog.h


this outputted some C++ code, my guess was that this was the meta-object compiled code, which compilers can now understand and doesn't contain anything Qt specific [am i right?]. So, I got the output in a file using:

code:
1:
moc finddialog.h > finddialog2.h

And modify the finddialog.cpp file a little:

code:
1:
#include "finddialog2.h"

instead of including "finddialog.h", it includes the moc-generated "finddialog2.h"...
Now.. all I had to do without modifying anything else was to add finddialog2.h to my project in Dev-C++ [actually.. it worked even if i didn't add it], and it compiled fine.. hooray...

now the only thing is how to automate this process? It is a bit tedious to continuously run moc on every such file and then include the generated file in my project. Any idea on how to go about?

Thanks,
rohanprabhu

This post has been edited 3 time(s), it was last edited by rohanprabhu: 12.03.2008 16:46.

12.03.2008 16:04 rohanprabhu is offline Send an Email to rohanprabhu Search for Posts by rohanprabhu Add rohanprabhu to your Buddy List
T.M.F. T.M.F. is a male
Chief Software Engineer


Registration Date: 21.11.2006
Posts: 140

Level: 24 [?]
Experience: 75,857
Next Level: 79,247

3,390 points of experience needed for next level

Reply to this Post Post Reply with Quote Edit/Delete Posts Report Post to a Moderator       Go to the top of this page

As I know codeblocks has some problems with Qt programs, you can use this IDE, but check the option to use externally generated makefiles (that one which is generated by qmake).
At first run qmake -pro to generate project file (with .pro extension). This is normal text file, so you can edit it. Take a look at examples how this fille looks like. After that run qmake to generate Makefile. At the end just run make to compile your programm. The qmake programm must be used to generate makefiles every time you will add/delete files to your project. You don't need to run moc manually or change header files. Makefile will do it instead of you.
13.03.2008 02:41 T.M.F. is offline Send an Email to T.M.F. Homepage of T.M.F. Search for Posts by T.M.F. Add T.M.F. to your Buddy List
rohanprabhu rohanprabhu is a male
Assistant


Registration Date: 11.03.2008
Posts: 4

Level: 6 [?]
Experience: 266
Next Level: 282

16 points of experience needed for next level

Thread Starter Thread Started by rohanprabhu
Reply to this Post Post Reply with Quote Edit/Delete Posts Report Post to a Moderator       Go to the top of this page

the problem is that if i run make on a qmake generated Makefile.. i get the following error:

code:
1:
make: *** No rule to make target `qt-cpp.pro', needed by `Makefile'. Stop.


Hence, if i manually run moc on the required files and then add the files to my project [or simply compile them in my Makefile and include the compiled .o files to the linker], I am able to build the program...

How do i make the error above to go away?
13.03.2008 04:23 rohanprabhu is offline Send an Email to rohanprabhu Search for Posts by rohanprabhu Add rohanprabhu to your Buddy List
rben13 rben13 is a male
Assistant


Registration Date: 09.05.2008
Posts: 1
Location: Boston

Level: 1 [?]
Experience: 7
Next Level: 10

3 points of experience needed for next level

Reply to this Post Post Reply with Quote Edit/Delete Posts Report Post to a Moderator       Go to the top of this page

Go into the project properities and tell it you are using a custom Makefile. That fixed that problem for me. Now i have a new one, but might be unrelated.
09.05.2008 19:02 rben13 is offline Send an Email to rben13 Homepage of rben13 Search for Posts by rben13 Add rben13 to your Buddy List
Tree Structure | Board Structure
Jump to:
Post New Thread Post Reply
Qt Forum - programming with C++ and Qt » QtForum.org » Qt » Qt Programming » Multiple Declaration + Endless Linker Errors

views today: 9.479 | views yesterday: 17.520 | total views: 10.242.848


Klebekork Shop - Linux Shop - Kontaktanzeigen - Linux Forum -  SMS Gewinnspiel -  Hotels -  Stadtpläne -  Branchenbuch & Stadtplan

Branchenbuch Österreich - Branchenbuch Niederlande - Portugal Branchenverzeichnis - Spanien Branchenverzeichnis 
Telefonbuch - Branchenbuch Frankreich