Q_OBJECT linker error!

被刻印的时光 ゝ 提交于 2020-01-10 18:20:21

问题


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?


回答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!




回答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



回答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.




回答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.




回答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.




回答6:


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.




回答7:


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



回答8:


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




回答9:


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




回答10:


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




回答11:


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



来源:https://stackoverflow.com/questions/3264259/q-object-linker-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!