Qt Vs addin - .obj files are not generated for moc_*.cpp files

有些话、适合烂在心里 提交于 2019-12-25 01:55:51

问题


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"/>

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

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?

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>

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?


回答1:


In Sloution Explorer, check

Generated Files

debug(or release which error happans)

moc_[yourErrorFile].cpp 

right click to it to see the propertise

if General->Exclude From Build is True Change it to False

this is one possibility will error happen




回答2:


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

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




回答3:


that is due to space in project folder... my project/

rename to my-project/

thats all folks...



来源:https://stackoverflow.com/questions/28455525/qt-vs-addin-obj-files-are-not-generated-for-moc-cpp-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!