PDA

View Full Version : Q_OBJECT macro and link errors



pscheven
21st March 2008, 09:32
Hello.

When I want to use the Q_OBJECT macro in one of my classes, I have link errors saying "unresolved external symbol" :


main.obj : error LNK2001: symbole externe non résolu "public: virtual struct QMetaObject const * __cdecl Recepteur::metaObject(void)const " (?metaObject@Recepteur@@UBAPBUQMetaObject@@XZ)
main.obj : error LNK2001: symbole externe non résolu "public: virtual void * __cdecl Recepteur::qt_metacast(char const *)" (?qt_metacast@Recepteur@@UAAPAXPBD@Z)
main.obj : error LNK2001: symbole externe non résolu "public: virtual int __cdecl Recepteur::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Recepteur@@UAAHW4Call@QMetaObject@@H PAPAX@Z)

There's no error when I compile the same code without the Q_OBJECT macro.

Here's the code of my very simple class :


class Recepteur : public QObject
{
Q_OBJECT

public :

Recepteur(){};

public slots:
void ButtonPushedReceived()
{
printf("Le bouton a été poussé");
}
};

Thank you for your answers.

Patrick Schevenels

Stukfruit
21st March 2008, 10:05
You have to compile and link with the generated file from the meta object compiler (moc) as well.

(Q_OBJECT defines a bunch of methods for which the implementation is in that generated file, that's why you're getting linker errors)

pscheven
21st March 2008, 10:09
Thank you, but in Visual Studio 2005, isn't it done automatically ?

If it is not done automatically, can you tell me how to do it please ?

I've joined my very simple source code (2 classes in .h and .cpp files + 1 main) where I have the problem.

Thank you for your help.

Patrick Schevenels

Stukfruit
21st March 2008, 10:37
Afaik it's only done automatically with the full VS integration, but that's for commercial Qt users only.
The Eclipse plugin does it (somewhat) automatically as well.

As a non-commercial / open source user you can use qmake to generate project files for VS. If you don't want to use qmake, try to make a simple test project and look into the custom build step for a header file. That will give you enough information for executing moc as a custom build step yourself, without using qmake. Remember though, that for this to work, you will have to include the generated file (from the meta object compiler).

jpn
21st March 2008, 13:23
Make sure all header files are listed in .pro file and re-generate the .vcproj by running "qmake -tp vc". You must always re-run qmake after adding or removing Q_OBJECT macro. This will ensure that custom build steps for running moc are being updated properly.