C++ Qt元对象链接器问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6642823/
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
Qt metaObject linker problem
提问by smallB
After integrating Qt with Vs and trying to compile .pro file I'm getting following errors:
在将 Qt 与 Vs 集成并尝试编译 .pro 文件后,我收到以下错误:
Error 9 error LNK2001: unresolved external symbol "public: virtual int __thiscall Multiplication_dialog::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Multiplication_dialog@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
Error 7 error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Multiplication_dialog::metaObject(void)const " (?metaObject@Multiplication_dialog@@UBEPBUQMetaObject@@XZ)
Error 8 error LNK2001: unresolved external symbol "public: virtual void * __thiscall Multiplication_dialog::qt_metacast(char const *)" (?qt_metacast@Multiplication_dialog@@UAEPAXPBD@Z)
What to do with this?
怎么办?
回答by jobor
You usally get these errors when the moc_foo.cpp for foo.h (which contains your class marked with Q_OBJECT) is not compiled / linked in your project.
当 foo.h(包含用 Q_OBJECT 标记的类)的 moc_foo.cpp 在您的项目中未编译/链接时,您通常会收到这些错误。
To make a Qt project work in VS you either
要使 Qt 项目在 VS 中工作,您要么
- Create a .vcproj file with 'qmake -tp vc' or
- Use the Qt Visual Studio Add-in which handles all the moc magic automatically for you (doesn't work with VC Express versions though).
- 使用 'qmake -tp vc' 创建一个 .vcproj 文件或
- 使用 Qt Visual Studio 插件,它会自动为您处理所有的 moc 魔法(但不适用于 VC Express 版本)。
When using the add-in you can trigger the creation of the moc_foo.cpp by
使用加载项时,您可以通过以下方式触发 moc_foo.cpp 的创建
- Make sure the header file of the object in question appears in the VS project
- List item
- remove all occurrances of Q_OBJECT from the header file of Multiplication_dialog.
- save the file
- add Q_OBJECT again
- save the file
- 确保有问题的对象的头文件出现在 VS 项目中
- 项目清单
- 从 Multiplication_dialog 的头文件中删除所有出现的 Q_OBJECT。
- 保存文件
- 再次添加 Q_OBJECT
- 保存文件
Now you should have two versions of moc_multiplication_dialog.cpp in your "Generated Files" folder in the Solution Explorer. One for "Debug" and one for "Release". Make sure that one of these files is notexcluded from build.
现在您应该在解决方案资源管理器的“生成的文件”文件夹中有两个版本的 moc_multiplication_dialog.cpp。一种用于“调试”,一种用于“发布”。确保这些文件之一未从构建中排除。
回答by michael_s
I faced the same linker error today, but it was due to a small slip:
我今天遇到了同样的链接器错误,但这是由于一个小错误:
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
因此,如果有人再次遇到同样的问题(或犯了同样的复制和粘贴错误): 确保头文件也已添加到您的项目文件中
回答by Grunelf
Citate from book "C++ GUI Programming with Qt 4" (page 19): For moc to work correctly, we must put the class definition in a header file, separate from the implementation file. So, you need write 2 files for your class: Multiplication_dialog.h and Multiplication_dialog.cpp! And you must recreate makefile!
引用“C++ GUI Programming with Qt 4”一书(第 19 页):为了使 moc 正常工作,我们必须将类定义放在一个头文件中,与实现文件分开。所以,你需要为你的类编写 2 个文件:Multiplication_dialog.h 和 Multiplication_dialog.cpp!你必须重新创建makefile!
回答by Neel Basu
Well Today I faced probably the same problem. I know the thread is pretty old. But It may still help someone.
那么今天我可能面临同样的问题。我知道线程已经很老了。但它可能仍然帮助某人。
What happened in my case was moc
was generating the moc_ .cpp
files but VC doesn't know that It has to compile them too. So I manually added those moc generated files so that it compiles. and It worked.
在我的情况下发生的moc
是生成moc_ .cpp
文件,但 VC 不知道它也必须编译它们。所以我手动添加了那些 moc 生成的文件以便编译。它起作用了。
回答by John Aggrey
I'm currently working with VS 2013 with QT 5.4 add-in. Building projects with the add-in makes it easier as the moc'ing is automatically handled. To prevent linker error problems with the Qt metaObject issues:
我目前正在使用带有 QT 5.4 插件的 VS 2013。使用加载项构建项目更容易,因为模拟是自动处理的。为了防止 Qt 元对象问题的链接器错误问题:
Comment out all instances of Q_OBJECT in all header files that contain it in the class declarations.
Build the solution
Uncomment all instances of the Q_OBJECT in the header files.
Rebuild the solution. This is where the all the all the header and cpp files are re-compiled with the moc'ing automatically done. The linker errors are in turn, taken care of.
在类声明中包含它的所有头文件中注释掉 Q_OBJECT 的所有实例。
构建解决方案
取消注释头文件中 Q_OBJECT 的所有实例。
重建解决方案。这是所有头文件和 cpp 文件重新编译的地方,moc'ing 自动完成。链接器错误依次得到处理。
回答by Ak?n Y?lmaz
Same problem occurred when I used Qt Addin for VS, I saw moc files are not included into project
after building. I included them into project these errors are gone.
当我将 Qt Addin 用于 VS 时发生了同样的问题,我are not included into project
在构建后看到了 moc 文件。我将它们包含在项目中,这些错误消失了。
回答by Patapoom
Try to relaunch the .proof your project.
尝试重新启动项目的.pro。
回答by Richard Jessop
Some fairly long-winded explanations above.
Using Qt Creator, on the menu bar:
Build>Run qmake
Build>Build All
上面一些相当冗长的解释。
使用 Qt Creator,在菜单栏上:
Build>Run qmake
Build>Build All