views:

79

answers:

3

Hi,

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?

A: 

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.

rubenvb
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
@rubenvb, 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
+3  A: 

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
@chalup, 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
Delete by hand all files generated by qmake and all object files, then rerun qmake.
chalup
@ 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
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. qmake3. make4. Add Q_OBJECT to the class.5. make6. Same errors.7. qmake8. make9. 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
A: 

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 compile !

Good Luck !

Carlos Nieto