I have several Visual Studio projects created with the Qt Visual Studio Tools (always the latest version available at the moment of the project creation, some dating from the first versions supporting Qt 5, now is the 2.2.1). All projects are compiled with VS 2010, although the IDE is VS 2017 (15.7.4 so far).
From some time on, some projects started to 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.
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).
Has anybody experienced the same issue? Any ideas how to solve it?
Update: 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.
As it seems to be a bug in the Qt Visual Studio Tools with no workaround, I've reported it under QTVSADDINBUG-555.