Q_OBJECT linker error!

27,539

Solution 1

Such errors usually mean that you haven't added the header of your class to "HEADERS" variable in pro file (meta object compiler generates moc_ files only for headers listed in this variable). Remember to run qmake after you change .pro file!

Solution 2

I had a similar problem and it was solved using andref's feedback. Within QT Creator I simply:

  1. Build/Clean all
  2. Build/Run qmake
  3. Build/Run

Solution 3

Whenever you change QObject inheritance be sure to do a clean, qmake then build. The qmake is important since it updates moc* files for any new Qt changes in your .h files including QObject inheritance, ie Q_OBJECT. In fact, in some cases, you may even be able to simply do qmake then build for an incremental build.

Solution 4

Check in the file MakeFile.debug and maybe HIMyClass don't exists.

I just rename MakeFile.debug, Clean the Project and Rebuild All and it compiles.

Solution 5

I had the same problem but in my case it wasn't enough to clean -> build. So I had to delete manually all files created in build process (Mekefiles, ui descriptionns in cpp, and generally whole directory created by build process) and only then build succeded.

Share:
27,539
liaK
Author by

liaK

Updated on April 21, 2020

Comments

  • liaK
    liaK about 4 years

    I am receiving the following linker error when I build my application.

    HIMyClass.obj:: error: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall CHIMyClass::metaObject(void)const " (?metaObject@CHIMyClass@@UBEPBUQMetaObject@@XZ) File not found : HIMyClass.obj

    HIMyClass.obj:: error: unresolved external symbol "public: virtual void * __thiscall CHIMyClass::qt_metacast(char const *)" (?qt_metacast@CHIMyClass@@UAEPAXPBD@Z) File not found : HIMyClass.obj

    HIMyClass.obj:: error: unresolved external symbol "public: virtual int __thiscall CHIMyClass::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@CHIMyClass@@UAEHW4Call@QMetaObject@@HPAPAX@Z) File not found : HIMyClass.obj

    My class declaration is like

    class CHIMyClass:public QDialog
    {
       Q_OBJECT
    
       ....
    
    };
    

    When I comment Q_OBJECT the linker error goes off (and obviously I am not able to use signals and slots). I am using Qt Creator as IDE and Qt 4.5.3. When I give Rebuild All it's definite that QMake will be called. I guess that, its the generation of moc_* files is where the problem lies. I am using Windows XP and cl as the compiler.

    What might be the reason behind this linker error?