0

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?

Akın Yılmaz
  • 300
  • 5
  • 18
  • Normally, if you have Qt VS plugin properly installed and your project defined to be a Qt project, adding a header file that contains Q_OBJECT declaration automatically adds corresponding moc_*.cpp to the `Generated Files` folders of the project. Maybe your project should be converted to Qt Add-in project? – vahancho Feb 11 '15 at 13:30
  • No project is a Qt addin project and moc_*.cpp files are generated into Generated Files folder, no problem in generation. The problem is generated moc_*.cpp file is not included into project and so not compiled and because of missing obj files these errors occur. – Akın Yılmaz Feb 11 '15 at 13:36
  • Generally, you need to add the file to the project to have it compiled... It is normal fair with qt to add the generated moc file to the project after its been created the first time. – g19fanatic Feb 11 '15 at 13:37
  • Yes this is the solution I found, but when moc_*.cpp files are deleted from one configuration and when I try to build from another configuration, build stops because deleted moc file is added to project but does not exist – Akın Yılmaz Feb 11 '15 at 13:38
  • @AkınYılmaz, strange. For me, they added automatically as soon as I add new Qt based header file. – vahancho Feb 11 '15 at 14:22
  • Try recreating the VS project from the .pro file. – sashoalm Feb 11 '15 at 14:59
  • See https://stackoverflow.com/questions/11941885/moc-ed-files-being-exluded-from-build-in-visual-studio-2010 and https://stackoverflow.com/questions/1200268/qt4-in-visual-studio-2008-moc-ed-files-get-excluded-from-build – sashoalm Feb 11 '15 at 16:05
  • What you are expecting to happen is what should happen. Have you tried uninstalling and reinstalling the VS Add-in? – RobbieE Feb 12 '15 at 06:57
  • I added a trial .h file as a qt class for seeing what happens when new qt header added in project file. – Akın Yılmaz Feb 12 '15 at 08:57
  • Just remove both .h and .cpp files and re-add them to your project. – Dmitry Sazonov Jul 21 '17 at 11:17

3 Answers3

0

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

newdazhu
  • 21
  • 2
0

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

marcbf
  • 139
  • 1
  • 2
  • 10
-1

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

rename to my-project/

thats all folks...

  • 1
    There's no folder called "my project" in the question. When the OP says "my project" they literally mean their project. – Blastfurnace Jul 22 '17 at 02:16