Qt Vs插件-未为moc _ *.cpp文件生成.obj文件 [英] Qt Vs addin - .obj files are not generated for moc_*.cpp files

查看:549
本文介绍了Qt Vs插件-未为moc _ *.cpp文件生成.obj文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Visual Studio与qt加载项一起使用,并且在构建项目时,由于我的项目中的构建规则,它会生成moc _ *.cpp文件:

I am using Visual Studio with qt addin and when I build my project, it generates moc_*.cpp files as a result of build rule in my project :

<Tool
Name="VCCustomBuildTool"
Description="Moc&apos;ing $(InputFileName)..."
CommandLine="$(QTDIR)\bin\moc.exe $(InputPath) -o .\GeneratedFiles\$(ConfigurationName)\moc_$(InputName).cpp (continues..)
AdditionalDependencies="$(QTDIR)\bin\moc.exe;.\myfilewithqobject.h"
Outputs=".\GeneratedFiles\$(ConfigurationName)\moc_myfilewithqobject.cpp"/>

生成moc_myfilewithqobject.cpp后,它将继续构建项目,并且在链接阶段会出现LINK 2001错误:

After generating moc_myfilewithqobject.cpp, it continues to build project and in linking phase it gives LINK 2001 errors :

unresolved external symbol "public: virtual struct QMetaObject const * __thiscall myfilewithobject::metaObject(void)const
unresolved external symbol "public: virtual void * __thiscall myfilewithobject::qt_metacast(char const *)
unresolved external symbol "public: virtual int __thiscall myfilewithobject::qt_metacall

因为找不到文件moc_myfilewithobject.obj. 由于生成后未编译moc_myfilewithobject.cpp,因此不会生成moc_myfilewithobject.obj. 但是,当我在项目中包含moc_myfilewithobject.cpp时,便对其进行了编译并解决了问题. 但这是手工完成的,有时会引起一些问题. 有没有一种方法可以解决此问题,而无需包含moc_ .cpp文件进行手动投影? 像刚生成后自动编译moc _ .cpp还是自动将其包括在内?

Because it cannot find the file moc_myfilewithobject.obj. moc_myfilewithobject.obj is not generated because moc_myfilewithobject.cpp is not compiled after generation. However when I include moc_myfilewithobject.cpp to my project,then it is compiled and the problem is solved. But this is done by hand and it sometimes causes some problems. Are there a way to solve this problem without including moc_.cpp files to project manually? Like compiling moc_.cpp after just generation or including it automatically?

注意: 我添加了一个试用版.h文件作为qt类,以查看在项目文件中添加新的qt标头时会发生什么.它添加了不属于我有问题的文件的行:

Note : I added a trial .h file as a qt class for seeing what happens when new qt header added in project file. It added the lines that are not owned by my problematic file :

<ClCompile Include="GeneratedFiles\Debug\moc_Denemeqt.cpp">
  <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
  <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>

所以我认为这是一个真实的配置,但是以前作为qt类创建的文件没有这样的配置.但是,当我删除GeneratedFiles中的Debug文件夹时,也会出现相同的错误,因为当我删除Debug文件夹时,moc_ .cpp文件也会从项目文件中删除. 所以这是一个问题:不能删除生成的moc _ .cpp文件,是真的吗?

So it is a true configuration I think, but my file created before as a qt class does not have such a configuration. However when I delete Debug folder in GeneratedFiles it gives same errors because when I delete Debug folder the moc_.cpp files also deleted from project file. So here is the question : generated moc_.cpp files must not be deleted, is it true?

推荐答案

@newdazhu出现了问题.

@newdazhu is on to something.

我也遇到过类似的情况,其中项目中的某些(但不是全部)类具有未解析的成员函数.事实证明,这是由于缺少某些对象文件(针对运动类)造成的.反过来,由于未将它们从当前配置的构建中排除,因此未构建这些文件.

I've just had a similar situation, where some, but not all, classes in a project had unresolved member functions. Turns out this was because of some missing object files (for moc'ed classes). These in turn were not built because they were excluded from build for the current configuration.

就我而言,它们在调试和发行版配置中都被排除在构建之外.这通常是一个错误,除非所讨论的源文件在末尾包含一个moc _ *.cpp文件(一种在Qt内部经常使用的技术,因此您可以在源文件中包含例如signal/slots).在那种情况下,您实际上必须排除这两种/所有配置,因为否则会得到一个乘以定义的错误(moc _ *.cpp已被编译到另一个目标文件中).

In my case they were excluded from build for both the debug and the release configuration. This is usually an error UNLESS the source file in question includes a moc_*.cpp file at the end (a technique often used internally in Qt, so you can have e.g. signals/slots in a source file). In that case you actually have to exclude both/all configurations, since you would otherwise get a multiply defined error (the moc_*.cpp is already compiled into the other object file).

这篇关于Qt Vs插件-未为moc _ *.cpp文件生成.obj文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆