Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt5 unresolved external to staticMetaObject function

Tags:

c++

qt

I have a class that is derived from QObject and QRunnable and also has the Q_OBJECT macro. The library that contains the class compiles fine and I get a .lib and .dll file. I'm using MSVC 2013 and QT 5.4 (precompiled binaries from qt.io).

Looking at the DLL using Dependency Walker, I can see that the function is there. The file get's moc'ed which means I can look at the resulting CPP file. As a proof here's the function that's causing the trouble.

const QMetaObject DHImageConvHandler::staticMetaObject = {
    { &QObject::staticMetaObject, qt_meta_stringdata_DHImageConvHandler.data,
      qt_meta_data_DHImageConvHandler,  qt_static_metacall, Q_NULLPTR, Q_NULLPTR}
};

Now, when I try to create an app that links to this library I get the following error.

unresolved external symbol "public: static struct QMetaObject const DHImageConvHandler::staticMetaObject" (?staticMetaObject@DHImageConvHandler@@2UQMetaObject@@B) 
referenced in function "public: static class QString __cdecl DHImageConvHandler::tr(char const *,char const *,int)" (?tr@DHImageConvHandler@@SA?AVQString@@PBD0H@Z)

Changing the project type for the library from "Dynamic Library" to "Static Library" makes this go away but I'm curious why. The code is in the lib. I opened the import library with a text editor and looked for staticMetaObject and, as mentioned previously, Dependency Walker also shows that it's there.

Can anyone shed some light on this?

edit 10.01.2015 I misspoke about who is using the library in question. That lib is linked to another lib which is then part of an app.

like image 227
Robert Lohr Avatar asked Jan 09 '15 13:01

Robert Lohr


1 Answers

Thanks Archie for pointing me into the right direction. The dllimport/dllexport prefixes are part of the code - wait for it - but every library is using the same macro and preprocessor directive. That means, when my problem library is used by code of another library, and both use the same macro to export their symbols, the second library includes the headers of the first library with dllexport instead of dllimport. As soon as I gave my problem lib its own dllexport/dllimport macro everything worked.

like image 66
Robert Lohr Avatar answered Nov 11 '22 03:11

Robert Lohr