17

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?

liaK
  • 11,422
  • 11
  • 48
  • 73
  • Maybe `File not found : HIMyClass.obj` tell us, that Qt Creator does not saw moc/obj/temp files? – mosg Jul 16 '10 at 11:39
  • 1
    Really, rerun qmake. Also, check for missing or extra "\" characters in your .pro file. – andref Jul 16 '10 at 17:22

11 Answers11

31

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!

chalup
  • 8,358
  • 3
  • 33
  • 38
  • The corresponding header file is available already under HEADERS variable still facing the problem though.. It's not the reason.. at least in my case.. :( – liaK Jul 16 '10 at 13:31
  • 2
    Delete by hand all files generated by qmake and all object files, then rerun qmake. – chalup Jul 16 '10 at 18:02
  • 1
    @ chalup, I deleted all the MOC, OBJ,RCC files and ran qmake, now it's working fine. While am glad that it worked, this is insane.. Why such an undefined behavior?? Any guess?? – liaK Jul 19 '10 at 05:00
  • 7
    It's not undefined. Actually, it's pretty deterministic: 1. Create a class that extends QObject but forget to add Q_OBJECT to it. 2. qmake 3. make 4. Add Q_OBJECT to the class. 5. make 6. Same errors. 7. qmake 8. make 9. No more errors. Qmake will only add a 'moc' rule to those files in HEADERS that have a class with the Q_OBJECT macro somewhere. – andref Jul 22 '10 at 15:47
13

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
sakisk
  • 1,807
  • 1
  • 24
  • 30
2

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.

Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
1

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.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
Tomek
  • 11
  • 1
1

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.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
0

on my osx box this was due to missing moc* files. i fixed this by removing the bom from my utf-8 encoded .pro file. i will file a bug with qt.

error for goggle searches... NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

"vtable for MainWindow", referenced from:

 MainWindow::MainWindow(QWidget*)in mainwindow.o
Community
  • 1
  • 1
jimmy
  • 1
0

I found another possible cause and solution to this error

This error will also occur if one has declared the slot in .h file but has not defined its body in the implementation

user3079474
  • 1,653
  • 2
  • 24
  • 37
0

I had this problem. Verify whether there is a description of the implementation of the slot in .cpp file.

RN3KK Nick
  • 701
  • 6
  • 12
0

Check that the necessary Qt config options are present in the pro file (QT += core gui at least. Also try manually deleting everything built/created in the build directory. It sometimes happens that moc fails to run for some reason.

You can also try running the moc command yourself, and see what it outputs (you can find the command line in the tab "Compile output" in QtCreator.

UPDATE: this related problem seems to suggest you don't define QT_DLL when compiling. Can you try a fresh and new simple QtCreator project (with a widget that subclasses mainwindow for example) and try that. It should contain a Q_OBJECT header automatically and try to compare the .pro files and compiler output.

Community
  • 1
  • 1
rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • It's not Qt symbols that's missing, but the moc-generated symbols for his own class. So I'd rather go with chalup and check for the header being listed in HEADERS. a completely clean build might also help. – Frank Osterfeld Jul 16 '10 at 12:57
  • It's linking fine all other classes that has Q_OBJECT macro. @Frank, I included the file in HEADERS section and tried clean builds more than once.. No luck though.. :( – liaK Jul 16 '10 at 13:29
0

I had removed #include "main.moc" from my main file, and forgot to re-add it... That was a fun time-waster!

kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80
0

Answers from @chalup and @ierax helped me. I had to close the Qt creator and open it again though for qmake to take effect. I followed these steps: 1. Moved the class definition to a header file. 2. Added header file to project and ensured it is listed against HEADERS += \ list in .pro file. 3. Clean-all 4. close QtCreator (on Windows 10) 5. Delete Makefiles from the project directory 6. Open QtCreator and open the project. 7. Qmake to ensure makefiles are generated. 8. Rebuild-all

These steps helped me solve this issue - I struggled for over an hour with various other answers and methods nothing worked. Before you run qmake ensure you delete makefiles and close QtCreator (applicable atleast on windows 10).

vinaymk
  • 84
  • 5