Q_OBJECT 链接器错误!

发布于 2024-09-10 04:19:15 字数 963 浏览 3 评论 0原文

当我构建应用程序时,我收到以下链接器错误。

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

HIMyClass.obj::错误: 未解析的外部符号“public: 虚空 * __thiscall CHIMyClass::qt_metacast(char 常量 *)" (?qt_metacast@CHIMyClass@@UAEPAXPBD@Z) 找不到文件:HIMyClass.obj

HIMyClass.obj::错误: 未解析的外部符号“public: 虚拟 int __thiscall CHIMyClass::qt_metacall(枚举 QMetaObject::Call,int,void * *)" (?qt_metacall@CHIMyClass@@UAEHW4Call@QMetaObject@@HPAPAX@Z) 找不到文件:HIMyClass.obj

我的类声明就像

class CHIMyClass:public QDialog
{
   Q_OBJECT

   ....

};

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

无法言说的痛 2024-09-17 04:19:15

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

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!

自此以后,行同陌路 2024-09-17 04:19:15

我遇到了类似的问题,并使用安德烈的反馈解决了这个问题。在 QT Creator 中,我只需:

  1. 构建/清理所有
  2. 构建/运行 qmake
  3. 构建/运行

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
ゃ人海孤独症 2024-09-17 04:19:15

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

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.

调妓 2024-09-17 04:19:15

检查文件MakeFile.debug,可能HIMyClass不存在。

我只需重命名 MakeFile.debug,清理项目并重新构建全部即可编译。

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.

安人多梦 2024-09-17 04:19:15

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

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.

独留℉清风醉 2024-09-17 04:19:15

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

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

更新: 相关 问题似乎表明您在编译时没有定义 QT_DLL 您可以尝试一个全新的简单 QtCreator 项目(例如带有子类化 mainwindow 的小部件)并尝试它应该自动包含 Q_OBJECT 标头并尝试进行比较。 .pro 文件和编译器输出。

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.

蹲墙角沉默 2024-09-17 04:19:15

在我的 osx 机器上,这是由于缺少 moc* 文件造成的。
我通过从 utf-8 编码的 .pro 文件中删除 bom 来修复此问题。
我将用 qt 提交一个错误。

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

“MainWindow 的 vtable”,引用自:

 mainwindow.o 中的 MainWindow::MainWindow(QWidget*)

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
浮生面具三千个 2024-09-17 04:19:15

我发现了此错误的另一种可能的原因和解决方案

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

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

り繁华旳梦境 2024-09-17 04:19:15

我遇到了这个问题。
验证.cpp文件中是否有该slot实现的描述。

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

避讳 2024-09-17 04:19:15

我已从主文件中删除了 #include "main.moc",但忘记重新添加它...这真是浪费时间!

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

安静被遗忘 2024-09-17 04:19:15

@chalup 和 @ierax 的回答对我有帮助。我必须关闭 Qt 创建器并再次打开它才能使 qmake 生效。我按照以下步骤操作:
1. 将类定义移至头文件中。
2. 将头文件添加到项目中,并确保它在 .pro 文件中的 HEADERS += \ 列表中列出。
3. 全部清理
4.关闭QtCreator(在Windows 10上)
5.删除项目目录下的Makefile
6.打开QtCreator并打开项目。
7. Qmake 确保生成 makefile。
8.重建所有

这些步骤帮助我解决了这个问题 - 我与各种其他答案和方法斗争了一个多小时,但没有任何效果。在运行 qmake 之前,请确保删除 makefile 并关闭 QtCreator(至少适用于 Windows 10)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文