C++ Q_OBJECT 链接器错误!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3264259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:28:17  来源:igfitidea点击:

Q_OBJECT linker error!

c++qtqt4linker-errorsqobject

提问by liaK

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

HIMyClass.obj:: 错误:未解析的外部符号“public: virtual struct QMetaObject const * __thiscall CHIMyClass::metaObject(void)const” (?metaObject@CHIMyClass@@UBEPBUQMetaObject@@XZ) 找不到文件:HIMyClass.obj

HIMyClass.obj:: 错误:未解析的外部符号“public: virtual void * __thiscall CHIMyClass::qt_metacast(char const *)” (?qt_metacast@CHIMyClass@@UAEPAXPBD@Z) 文件未找到:HIMyClass.obj

HIMyClass.obj:: 错误:未解析的外部符号“public: virtual int __thiscall CHIMyClass::qt_metacall(enum QMetaObject::Call,int,void * *)” (?qt_metacall@CHIMyClass@@UAEHW4Call@QMetaObject@@HPAPAX@Z)找不到文件: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 Allit's definite that QMakewill 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.

当我评论 Q_OBJECT 时,链接器错误消失(显然我无法使用信号和插槽)。我使用 Qt Creator 作为 IDE 和 Qt 4.5.3。当我提供Rebuild All 时,肯定会调用QMake。我想,它的 moc_* 文件的生成是问题所在。我使用 Windows XP 和 cl 作为编译器。

What might be the reason behind this linker error?

此链接器错误背后的原因可能是什么?

回答by chalup

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!

此类错误通常意味着您尚未将类的标题添加到 pro 文件中的“HEADERS”变量(元对象编译器仅为此变量中列出的标题生成 moc_ 文件)。更改 .pro 文件后记得运行 qmake !

回答by sakisk

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

我有一个类似的问题,它使用andref的反馈解决了。在 QT Creator 中,我只是:

  1. Build/Clean all
  2. Build/Run qmake
  3. Build/Run
  1. 构建/清理所有
  2. 构建/运行 qmake
  3. 构建/运行

回答by Stephen Quan

Whenever you change QObject inheritance be sure to do a clean, qmakethen build. The qmakeis 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 qmakethen buildfor an incremental build.

每当您更改 QObject 继承时,请务必执行 clean,qmake然后build. 这qmake很重要,因为它会针对 .h 文件中的任何新 Qt 更改更新 moc* 文件,包括 QObject 继承,即Q_OBJECT. 事实上,在某些情况下,您甚至可以简单地为增量构建执行qmakethen build

回答by Carlos Nieto

Check in the file MakeFile.debugand maybe HIMyClassdon't exists.

签入文件MakeFile.debug,可能HIMyClass不存在。

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

我只是重命名MakeFile.debug,清理项目并重新构建所有并编译。

回答by Tomek

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.

我遇到了同样的问题,但就我而言,清理 -> 构建还不够。所以我不得不手动删除在构建过程中创建的所有文件(Mekefiles、cpp 中的 ui descriptionns,以及通常由构建过程创建的整个目录),然后构建成功。

回答by jimmy

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.

在我的 osx 盒子上,这是由于缺少 moc* 文件。我通过从我的 utf-8 编码的 .pro 文件中删除 bom 来解决这个问题。我会用qt提交一个错误。

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

护目镜搜索错误... 注意:缺少 vtable 通常意味着第一个非内联虚拟成员函数没有定义。

"vtable for MainWindow", referenced from:

 MainWindow::MainWindow(QWidget*)in mainwindow.o

“主窗口的 vtable”,引用自:

 MainWindow::MainWindow(QWidget*)in mainwindow.o

回答by rubenvb

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

检查 pro 文件中是否存在必要的 Qt 配置选项(QT += core gui至少。还尝试手动删除构建目录中构建/创建的所有内容。有时会发生moc由于某种原因无法运行的情况。

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.

您也可以尝试自己运行 moc 命令,并查看它输出的内容(您可以在 QtCreator 的“编译输出”选项卡中找到命令行。

UPDATE: thisrelatedproblem 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.

更新:这个相关问题似乎表明您在编译时不要定义 QT_DLL。您能否尝试一个全新的简单 QtCreator 项目(例如,使用一个子类化 mainwindow 的小部件)并尝试一下。它应该自动包含一个 Q_OBJECT 标头并尝试比较 .pro 文件和编译器输出。

回答by user3079474

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

如果在 .h 文件中声明了插槽但没有在实现中定义它的主体,也会发生此错误

回答by RN3KK Nick

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

我有这个问题。验证 .cpp 文件中是否有插槽实现的描述。

回答by kayleeFrye_onDeck

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

我已经#include "main.moc"从我的主文件中删除了,忘记重新添加它......那是一个有趣的浪费时间!