Uploaded image for project: 'Qt Visual Studio Tools'
  1. Qt Visual Studio Tools

Moc'ed file gets mysteriously excluded from Visual Studio project



    • Bug
    • Resolution: Out of scope
    • P3: Somewhat important
    • None
    • 2.0.0, 2.2.0, 2.2.1
    • General
    • None
    • Visual Studio 2017 for IDE, Visual Studio 2010 for toolset
    • Windows


      Some projects report link errors such as

      error LNK2001: unresolved external symbol "public: static struct QMetaObject const MyQtClass::staticMetaObject" (?staticMetaObject@MyQtClass@@2UQMetaObject@@B)

      For this example, MyQtClass.h file declares MyQtClass and has the Q_OBJECT macro. MyQtClass.cpp defines the methods.

      After a quick inspection, I discovered that the problem came from fact that the associated moc'ed file (moc_MyQtClass.cpp for above example) was excluded from compilation for current configuration. Here the extract of the .vcxproj file:

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

      If I modify the .vcxproj file to remove the exclusion from the Debug configuration, then the project works.

      The problem only happens when the main .cpp file associated to the Q_OBJECT}}ed class is modified ({{MyQtClass.cpp in my example). At the time such file is saved, the .vcxproj file is modified with the exclusions.

      The issue only happens when the modified file is the .cpp file with identical basename regarding the header file (MyQtClass.cpp in my example). If I modified another file that also defines more methods of the MyQtClass class (such as MyQtClass_more_definitions.cpp), no error is given. It doesn't matter what the modification implies: it can be a single space added.

      This issue doesn't appears in every Qt project I have, but once it starts it keeps appearing every time the associated .cpp file is modified. I haven't been able to discover a pattern for it. In addition, it happens in several development systems at the company, so it doesn't seem to be a problem of my computer.

      The only workaround I've found is to discard the changes from the project file, but it is a headache to be discarding and relaunching the project every time those files are modified (which happens to be a lot of times).

      I've also modified the projects as can be seen below. It doesn't solve the issue but helps when reverting changes.

      <ClCompile Include="GeneratedFiles\$(Configuration)\moc_MyQtClass.cpp" />


        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.



              mecfc Miguel Costa
              cbuchart Carlos Buchart
              0 Vote for this issue
              3 Start watching this issue



                Gerrit Reviews

                  There are no open Gerrit changes