Saturday, July 5th 2008, 5:56am UTC+1

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, March 11th 2008, 10:27am

Multiple Declaration + Endless Linker Errors

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:

Quoted


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


at this line:

Source code

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

Quoted


[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
  • Go to the top of the page

2

Wednesday, March 12th 2008, 2:52am

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.
  • Go to the top of the page

3

Wednesday, March 12th 2008, 4:20am

first of all.. thanks a lot for replying.

Quoted

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.

Quoted

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:

Quoted


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:

Quoted


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


Thanks,
rohanprabhu

This post has been edited 1 times, last edit by "rohanprabhu" (Mar 12th 2008, 4:25am)

  • Go to the top of the page

4

Wednesday, March 12th 2008, 3:04pm

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

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

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

Source code

1
moc finddialog.h > finddialog2.h

And modify the finddialog.cpp file a little:

Source 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 times, last edit by "rohanprabhu" (Mar 12th 2008, 3:46pm)

  • Go to the top of the page

5

Thursday, March 13th 2008, 1:41am

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.
  • Go to the top of the page

6

Thursday, March 13th 2008, 3:23am

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

Source 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?
  • Go to the top of the page

rben13

Beginner

Posts: 1

Location: Boston

7

Friday, May 9th 2008, 6:02pm

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.
  • Go to the top of the page

Rate this thread