C++ Qt 并且没有 moc_*.cpp 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/177468/
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-27 13:27:39  来源:igfitidea点击:

Qt and no moc_*.cpp file

c++qtqt4

提问by ?ukasz Bownik

I'm developing a simple Qt 4 app and making my own dialog. I subclassed QDialog, inserted the Q_OBJECTmacro in the class declaration block, and... I get

我正在开发一个简单的 Qt 4 应用程序并制作我自己的对话框。我子类化QDialogQ_OBJECT在类声明块中插入宏,然后......我得到

[Linker error] undefined reference to `vtable for MyDialog' and there is no moc_MyDialog.cpp generated by the moc compiler.

[链接器错误] 未定义对“MyDialog 的 vtable”的引用,并且没有由 moc 编译器生成的 moc_MyDialog.cpp。

I am using Qt 4.1.3 on Windows XP and mingw. I followed the build process from the Qt-supplied build shell. I used qmake to create make files and compiled everything with a make command.

我在 Windows XP 和 mingw 上使用 Qt 4.1.3。我遵循了 Qt 提供的构建 shell 中的构建过程。我使用 qmake 创建 make 文件并使用 make 命令编译所有内容。

I have other classes that subclass QPushButtonand QObjectrespectively, but they compile OK. I can't find any differences between them and the broken one.

我有其他类分别是子类QPushButtonQObject,但它们编译正常。我找不到它们和破碎的之间的任何区别。

There must be missing something in the broken class, but I'm unable to spot it.

坏掉的班级里肯定少了点什么,但我找不到。

回答by David Dibben

The undefined reference to "vtable for MyDialog" is caused because there is no moc file. Most c++ compilers create the vtable definition in the object file containing the first virtual function. When subclassing a qt object and using the Q_OBJECT macro, this will be in the moc*.cpp file. Therefore, this error means that the moc file is missing.

对“vtable for MyDialog”的未定义引用是因为没有 moc 文件。大多数 c++ 编译器在包含第一个虚函数的目标文件中创建 vtable 定义。当子类化一个 qt 对象并使用 Q_OBJECT 宏时,这将在 moc*.cpp 文件中。因此,此错误意味着缺少 moc 文件。

The possible problems I can think of are:

我能想到的可能的问题是:

  1. The header file for the class MyDialog.h is not added to HEADERS in the qmake file.

  2. You ran qmake to generate the make file beforeadding the Q_OBJECT macro. This created a make file without the moc rules. This is easily fixed by simply running qmake again.

  3. Your dialog derives from more than one class and QDialog is not the first class that it derives from. For qmake to work correctly, the QObject derived base class needs to be the first class that is inherited from.

  4. If you are using Qt Creator, you might get this error if your previous deployment was failed due to some reason (like application already running). In that case, simply do a 'Clean Project' and then 'Rebuild Project' and then 'Run' to deploy.

  1. 类 MyDialog.h 的头文件没有添加到 qmake 文件中的 HEADERS 中。

  2. 添加 Q_OBJECT 宏之前,您运行 qmake 来生成 make 文件。这创建了一个没有 moc 规则的 make 文件。只需再次运行 qmake 即可轻松解决此问题。

  3. 您的对话框派生自多个类,而 QDialog 不是它派生的第一个类。为了使 qmake 正常工作,QObject 派生基类需要是第一个继承的类。

  4. 如果您使用的是 Qt Creator,如果您之前的部署由于某种原因(例如应用程序已经在运行)而失败,您可能会收到此错误。在这种情况下,只需执行“清理项目”,然后“重建项目”,然后“运行”即可部署。

回答by Kamrul

If you have your header file included, follow the steps:

如果包含头文件,请执行以下步骤:

  1. Right click on the project where you have added this.
  2. Hit 'Run qmake'.
  1. 右键单击您添加此项目的项目。
  2. 点击“运行 qmake”。

This will will clear up the old references and build with th Q_OBJECT macro. QT doesn't do it on rebuild.

这将清除旧引用并使用 Q_OBJECT 宏进行构建。QT 在重建时不会这样做。

回答by Bemipefe

I have see that the problem appear only when is added a class with no extension and then an extension is added manually.

我已经看到只有在添加一个没有扩展名的类然后手动添加一个扩展时才会出现问题。

To solve the problem i put Q_OBJECT in the .h of the class and then right click on "Sources" -> "Add Existing Files..." selecting the .ccp of my modified class.

为了解决这个问题,我将 Q_OBJECT 放在类的 .h 中,然后右键单击“源”->“添加现有文件...”选择我修改后的类的 .ccp。

回答by jelle foks

The message undefined reference to `vtable for MyDialog' can also be the result of a missing implementation (in MyDialog) of a pure virtual function in a class that MyDialog is derived from.

消息 undefined reference to `vtable for MyDialog' 也可能是 MyDialog 派生自的类中的纯虚函数缺少实现(在 MyDialog 中)的结果。

回答by user24560

Are you using qmake? Perhaps you didn't add it to your the .cpp file to your SOURCES and .h file to your HEADERS variable in the qmake file?

你在用qmake吗?也许您没有将它添加到您的 .cpp 文件到您的 SOURCES 和 .h 文件到您的 qmake 文件中的 HEADERS 变量?

回答by Marcin Gil

I humbly suggest you use CMakefor building Qt programs on Windows. It will keep you remember to add appropriate files to its build files.

我虚心建议您使用CMake在 Windows 上构建 Qt 程序。它会让您记住将适当的文件添加到其构建文件中。

The additional value is that you can generate make/nmake build files from it, Visual Studio solution files. And if you compile Qt from source for Visual Studio you will be able to both code and build with MS IDE/compiler.

额外的价值是您可以从中生成 make/nmake 构建文件,即 Visual Studio 解决方案文件。如果您从 Visual Studio 的源代码编译 Qt,您将能够使用 MS IDE/编译器进行编码和构建。

This is of course if you are using Visual Studio at all.

这当然是在您使用 Visual Studio 的情况下。