C++ 未解析的外部符号“public: virtual struct QMetaObject const * __thiscall Parent

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

Unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Parent

c++qtqobject

提问by sayyed mohsen zahraee

I inherited a class from QObject :

我从 QObject 继承了一个类:

class Parent: public QObject
{
    Q_OBJECT
    QObject* cl;

public:
    Parent(QObject *parent=0):QObject(parent) {
        cl = NULL;
    }

    QObject* getCl() const {
        return cl;
    }
    void setCl(QObject *obj) {
        cl = obj;
    }
};

But when I write :

但是当我写:

Parent ev;

I get the following error:

我收到以下错误:

main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Parent::metaObject(void)const " (?metaObject@Parent@@UBEPBUQMetaObject@@XZ)

main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __thiscall Parent::qt_metacast(char const *)" (?qt_metacast@Parent@@UAEPAXPBD@Z)

main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __thiscall Parent::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Parent@@UAEHW4Call@QMetaObject@@HPAPAX@Z)

回答by sayyed mohsen zahraee

You should delete the debugfolder of your application and run it again to correct this problem.

您应该删除debug应用程序的文件夹并再次运行它以更正此问题。

回答by MPicazo

If you're using Visual Studio, delete the line Q_OBJECTfrom the header file, save the file, put Q_OBJECTback into the header file, save the file again. This should generate the moc_*file and should build and link correctly.

如果您使用的是 Visual Studio,请Q_OBJECT从头文件中删除该行,保存文件,放Q_OBJECT回头文件中,再次保存文件。这应该会生成moc_*文件并且应该正确地构建和链接。

回答by Stephen Quan

I noticed some answers are based on Visual Studio.

我注意到一些答案基于 Visual Studio。

This answer is based on Qt Creator.

这个答案基于 Qt Creator。

Unlike the name suggest, Rebuild Projectwill not wipe out everything and build from scratch. If you recently added QObject(and/or Q_OBJECT) to your class, you'll have to run qmakeagain, e.g.

不像顾名思义,Rebuild Project不会消灭一切并从头开始构建。如果您最近将QObject(和/或 Q_OBJECT)添加到您的班级,则必须qmake再次运行,例如

  1. Clean Project
  2. Run qmake
  3. Build Project
  1. 清洁工程
  2. 运行 qmake
  3. 构建项目

This is because, by default, qmakeonly runs when you do significant changes to your solution like adding new source files or modify the .profile. If you make edits to an existing file, it doesn't know it needs to run qmake.

这是因为,默认情况下,qmake只有在您对解决方案进行重大更改(例如添加新源文件或修改.pro文件)时才会运行。如果您对现有文件进行编辑,它不知道它需要运行qmake.

As a fall back, to brute force Qt to build everything from scratch, delete the Debugor Releasefolder.

作为后退,要强制 Qt 从头开始​​构建所有内容,请删除DebugRelease文件夹。

回答by Vern Jensen

So the issue was I needed the Qt MOC compiler to compile my .h file. This is required for any classes that extend QObject or one of its children. The fix involed (for me) right-clicking on the header file, choosing Properties, and setting the Item Type to "Qt MOC Input", then hitting "Compile" on the header, and then adding the resulting moc_myfilename.cpp file to my project.

所以问题是我需要 Qt MOC 编译器来编译我的 .h 文件。这是扩展 QObject 或其子级之一的任何类所必需的。修复涉及(对我而言)右键单击头文件,选择“属性”,并将“项目类型”设置为“Qt MOC 输入”,然后在头上点击“编译”,然后将生成的 moc_myfilename.cpp 文件添加到我的项目。

回答by Ak?n Y?lmaz

If your moc files are generated in the visual studio project try to include them into project if they are not included into project then rebuild.

如果您的 moc 文件是在 Visual Studio 项目中生成的,请尝试将它们包含到项目中,如果它们未包含在项目中,则重新构建。

回答by Trevor Hickey

I had the same problem in Visual Studio, and solved it by taking the following steps:

我在 Visual Studio 中遇到了同样的问题,并通过以下步骤解决了它:

  1. Right-click header file in solution Explorer
  2. Properties
  3. Change "Item Type" to "Custom Build Tool"
  1. 在解决方案资源管理器中右键单击头文件
  2. 特性
  3. 将“项目类型”更改为“自定义构建工具”

Then in the Custom Build Tool configuration:

然后在自定义构建工具配置中:

  1. Go to General
  2. set "Command Line" to:

    "$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp" "-fStdAfx.h" "-f../../../src/FileName.h" -DUNICODE -DWIN32 -DWIN64 -DQT_DLL -DQT_NO_DEBUG -DNDEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB -DQT_NETWORK_LIB -DWIN32_LEAN_AND_MEAN -DDIS_VERSION=7 -D_MATH_DEFINES_DEFINED "-I.\SFML_STATIC" "-I.\GeneratedFiles" "-I." "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)." "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtNetwork"

  3. set "Outputs" to:

    .\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp

  4. set "Additional Dependencies" to:
    $(QTDIR)\bin\moc.exe;%(FullPath)

  1. 转到一般
  2. 将“命令行”设置为:

    "$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp" "-fStdAfx.h" "-f../ ../../src/文件名.h " -DUNICODE -DWIN32 -DWIN64 -DQT_DLL -DQT_NO_DEBUG -DNDEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB -DQT_NETWORK_LIB -DWIN32_LIB -DWIN32_LIB -DWIN32_LIB -DWIN32_FINED_FINED-FINED-FINED-FINED-FINED-FIND-FIND-FIND-FIND-FINID-HELION .\GeneratedFiles" "-I." "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)。" "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtNetwork"

  3. 将“输出”设置为:

    .\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp

  4. 将“附加依赖项”设置为:
    $(QTDIR)\bin\moc.exe;%(FullPath)



Your exact values may be different. They are usually applied via a Qt plugin.

您的确切值可能会有所不同。它们通常通过 Qt 插件应用。

回答by Edward

I had this problem with Visual Studio 2012 when I had a Q_OBJECT class definition in my cpp file. Moving the class definition to the header file resolved the issue.

当我的 cpp 文件中有 Q_OBJECT 类定义时,我在 Visual Studio 2012 中遇到了这个问题。将类定义移动到头文件解决了这个问题。

It looks like it should be possible to support Q_OBJECT class in cpp file by adding the cpp file to moc but I did not try this.

看起来应该可以通过将 cpp 文件添加到 moc 来支持 cpp 文件中的 Q_OBJECT 类,但我没有尝试过。

回答by whatnick

I use CMake to manage Qt projects and the new Q_OBJECT needs to be added under the QT4_WRAP_CPP call. This will generate the moc_*.cxx for inclusion in the project and clean up the unresolved externals.

我使用 CMake 来管理 Qt 项目,并且需要在 QT4_WRAP_CPP 调用下添加新的 Q_OBJECT。这将生成包含在项目中的 moc_*.cxx 并清理未解析的外部文件。

回答by Darkproduct

My problem was that one of my files that used a Qt macro didn't get moc'ed. I found out, that the Qt Plugin for Visual Studio doesn't recognize the Q_NAMESPACEmacro and therefore doesn't add the file to the moc'ing list.

我的问题是我的一个使用 Qt 宏的文件没有被修改。我发现,用于 Visual Studio 的 Qt 插件无法识别Q_NAMESPACE宏,因此不会将文件添加到 moc'ing 列表。

So I used the solution from this answerto add the file to the mic'ing list:

所以我使用这个答案中的解决方案将文件添加到麦克风列表中:

You should find a .h file which has successfully generated "moc_*", and copy all the contents in "Custom Build Tool -> General" to the new .h file setting page.

你应该找到一个已经成功生成“moc_*”的.h文件,将“Custom Build Tool -> General”中的所有内容复制到新的.h文件设置页面。

Be careful with the different options for Debugand Release-Mode.

小心使用DebugRelease-Mode的不同选项。

After that, build your project.

之后,构建您的项目。

Build it once each in Debugand Release-Mode

Debugand Release-Mode 中各构建一次

Finally, add the generated "moc_*" file to your project.

最后,将生成的“moc_*”文件添加到您的项目中。

Now, "moc_filename.cpp" should be in Generated Files\Debugand Generated Files\Release.

现在,“moc_filename.cpp”应该在Generated Files\Debug和 中Generated Files\Release

Right click on each of them and change thair properties:

右键单击它们中的每一个并更改 thair 属性:

  • The file in Debug: Change configuration to Releaseand then change General->Excluded from buildto yes.
  • The file in Release: Change configuration to Debugand then change General->Excluded from buildto yes.
  • 中的文件Debug:将配置更改为Release,然后更改General->Excluded from buildyes.
  • 中的文件Release:将配置更改为Debug,然后更改General->Excluded from buildyes.

回答by Vinoj John Hosan

I added cpp/ui files to my project manually, but forgot to add the header file explicitly as header file. Now when compiling I got a similar error message as above and the moc_*.cpp file(s) were not generated in the debug (or release) directory of the build. That was not such an obvious mistake, qmake did not complain and other than the linker message I got no errors.

我手动将 cpp/ui 文件添加到我的项目中,但忘记将头文件显式添加为头文件。现在,在编译时,我收到了与上述类似的错误消息,并且在构建的调试(或发布)目录中未生成 moc_*.cpp 文件。这不是一个明显的错误,qmake 没有抱怨,除了链接器消息之外,我没有任何错误。

So if anyone encounters the same problem again (or makes the same copy & pase mistake): make sure the header files have also been added to your project file

因此,如果有人再次遇到同样的问题(或犯了同样的复制和粘贴错误):确保头文件也已添加到您的项目文件中