Results 1 to 5 of 5

Thread: Q_OBJECT macro and link errors

  1. #1
    Join Date
    Mar 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Q_OBJECT macro and link errors

    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 :

    Qt Code:
    1. class Recepteur : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public :
    6.  
    7. Recepteur(){};
    8.  
    9. public slots:
    10. void ButtonPushedReceived()
    11. {
    12. printf("Le bouton a été poussé");
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 

    Thank you for your answers.

    Patrick Schevenels
    Last edited by jpn; 21st March 2008 at 13:19. Reason: missing [code] and [quote] tags

  2. #2
    Join Date
    Feb 2008
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_OBJECT macro and link errors

    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)

  3. #3
    Join Date
    Mar 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro and link errors

    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
    Attached Files Attached Files

  4. #4
    Join Date
    Feb 2008
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_OBJECT macro and link errors

    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).

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Q_OBJECT macro and link errors

    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.
    J-P Nurmi

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.