Question

I was trying to build a QT project in QT Creator, but was getting link errors:

minecraftlauncher.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Launcher::MinecraftLauncher::metaObject(void)const " (?metaObject@MinecraftLauncher@Launcher@@UEBAPEBUQMetaObject@@XZ)
minecraftlauncher.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl Launcher::MinecraftLauncher::qt_metacast(char const *)" (?qt_metacast@MinecraftLauncher@Launcher@@UEAAPEAXPEBD@Z)
minecraftlauncher.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl Launcher::MinecraftLauncher::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MinecraftLauncher@Launcher@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
debug\Launcher.exe : fatal error LNK1120: 3 unresolved externals

For some reason, moc.exe, the meta-object compiler, was creating empty moc_*.cpp files, with the error message

:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/include/type_trait(0): Note: No relevant classes found. No output generated.

I tried running mocat the command line with the flags it had been run with in QT Creator:

C:\QtSDK\x64\bin\moc.exe -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I"c:\QtSDK\x64\include\QtCore" -I"c:\QtSDK\x64\include\QtGui" -I"c:\QtSDK\x64\include" -I"c:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include" -I"c:\QtSDK\x64\include\ActiveQt" -I"debug" -I"..\Launcher" -I"." -I"c:\QtSDK\x64\mkspecs\win32-msvc2005" -D_MSC_VER=1400 -DWIN32 ..\Launcher\minecraftlauncher.hpp -o debug\moc_minecraftlauncher.cpp

and it returned the same error. I tried removing several flags one by one, and narrowed it down to -I"c:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include". For some reason, when that directory was on the include path, the "No relevant classes found" error was shown and an empty .cpp file was generated. However, when that directory was not on the include path, moc.exe worked normally.

Due to the error message including :/Program Files (x86)/Microsoft Visual Studio 11.0/VC/include/type_trait(0), I tried renaming the type_traits file in that location so it wouldn't be recognised. When I did that, moc.exe worked normally again (but actual failed as type_traits is included somewhere in the standard library files I have included). So the error is being caused somewhere in type_traits

From what little I know about type_traits, I believe it has something to do with getting compile type information about an object. But I have no idea why it is causing the meta-object compiler to err. Why is it doing this? What is moc doing with type_traits, why is it and what is failing?

I will be very grateful for any insight anyone can provide, as this has been very puzzling for me.

The minecraftlauncher.hpp file:

#ifndef MINECRAFTLAUNCHER_HPP
#define MINECRAFTLAUNCHER_HPP

#include <QMainWindow>
namespace Launcher {
    class MinecraftLauncher : public QMainWindow
    {
        Q_OBJECT

    public:
        explicit MinecraftLauncher(QWidget *parent = 0);
        ~MinecraftLauncher();

    private:

    };
}

#endif // MINECRAFTLAUNCHER_HPP
Was it helpful?

Solution

This is most likely a moc bug exposed by C++11 headers. I bet you didn't recompile Qt from the sources, as that would similarly fail.

Visual Studio 11 is not supported by Qt. It's not even in tier2 support. The workaround is to use MSVC 2010 until the bug is fixed, or for you to fix moc and submit a patch :)

OTHER TIPS

I was getting a similar error but with Visual Studio 12 and Qt was direct from Qt. So Kuba Ober's answer did not work.

:/Program Files (x86)/Microsoft Visual Studio
12.0/VC/include/type_trait(0): Note: No relevant classes found. No output generated.

What did work was to surround references to Qt headers with (not) Q_MOC_RUN as shown below. Using the top portion of Daniel Mulcahy's code...

#ifndef MINECRAFTLAUNCHER_HPP
#define MINECRAFTLAUNCHER_HPP

#ifndef Q_MOC_RUN
#  include <QMainWindow>
#endif //Q_MOC_RUN

namespace Launcher {
class MinecraftLauncher : public QMainWindow
{
    Q_OBJECT
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top