View Full Version : different statically-linked metaObject across shared libraries: qobject_cast fails

20th March 2013, 11:02
Hello everyone, I'm new to this forum.
I've searched the solution of my problem with extensive googling and in this forum but I found only these threads http://www.qtcentre.org/threads/17408-qobject_cast-fails-across-dynamic-library-boundaries?highlight=qobject_cast and http://www.qtcentre.org/threads/17799-qobject_cast-lt-T-gt-in-shared-libraries?highlight=qobject_cast which are very similar to my problem. However following those threads I was not able to find the solution of my problem.

I am using Qt 5.0 under Windows 7 (32 bit) and I have replicated my problem within a simple Qt project as follows (for the sake of conciseness I do not post the full code, however I can post it if necessary). The main project is a subdirs one with:
- one common static library (commonlib.lib)
- one shared library (Lib.dll)
- one console application (TestApp.exe)

Inside commonlib.lib I have defined the following classes:

class myObject : public QObject;
class myDerived : public myObject;
class myDllBase : public myObject;

All these classes are virtual and have the Q_OBJECT macro properly defined at the beginning of the class declaration inside the commonlib.h file.

The projects that build Lib.dll and TestApp.exe are statically linked to commonlib.lib and DO NOT include commonlib.h in the *.pro file headers section. The shared library contains a single shared function that returns a myLib* object where

class myLib : public myDllBase;

so that I can get the pointer to myLib using QLibrary and resolving the function symbol at runtime.

In the main application I allocate a new myDerived object as follows:

myObject* l_pTemp = new myDerived;

If I do the same inside Lib.dll I obtain a different metaObject. I guess this is the reason why qobject_cast fails whenever I try to cast inside the *.dll an object allocated in the main app.

I need to use qobject_cast inside the *.dll since I need to share objects among different shared libraries. However, due to the problem I posted above I'm not able to do that.

Do you have any idea on how can I solve my problem?

Thank you!