[SOLVED] Moc and Q_DECL_EXPORT



  • Hello everybody

    I got a problem trying to build a dll with those linker errors :

    moc_BallsEffect.obj:-1: erreur : LNK2001: symbole externe non résolu "public: static struct QMetaObject const XEEL_ColorMixing_Effect::staticMetaObject" (?staticMetaObject@XEEL_ColorMixing_Effect@@2UQMetaObject@@B)

    moc_BallsEffect.obj:-1: erreur : LNK2019: symbole externe non résolu "public: virtual void * __thiscall XEEL_ColorMixing_Effect::qt_metacast(char const *)" (?qt_metacast@XEEL_ColorMixing_Effect@@UAEPAXPBD@Z) référencé dans la fonction "public: virtual void * __thiscall BallsEffect::qt_metacast(char const *)" (?qt_metacast@BallsEffect@@UAEPAXPBD@Z)

    moc_BallsEffect.obj:-1: erreur : LNK2019: symbole externe non résolu "public: virtual int __thiscall XEEL_ColorMixing_Effect::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@XEEL_ColorMixing_Effect@@UAEHW4Call@QMetaObject@@HPAPAX@Z) référencé dans la fonction "public: virtual int __thiscall BallsEffect::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@BallsEffect@@UAEHW4Call@QMetaObject@@HPAPAX@Z)

    I have two projects. The first one is an sdk (.lib and .dll) and exports a series of qt based classes.
    The second is another dll, which exposes a single method and of course use the sdk.

    Basically I got this class in the dll:

    @
    class BallsEffect : public XEEL_ColorMixing_Effect
    {
    Q_OBJECT
    ...
    }
    @

    And this one in the sdk:
    @
    class Q_DECL_EXPORT XEEL_ColorMixing_Effect : public QObject
    {
    Q_OBJECT
    ...
    }
    @

    I tried a lot of things, and I can't make it work so if anyone had already encountered this problem...


  • Moderators

    when you check the Qt code you will see that this is not how it is used in the Qt sources.
    Don't use Q_DECL_IMPORT/Q_DECL_EXPORT directly, since your implementation needs to export it but the code where you are using it needs to IMPORT it. So you telling the compiler to export something which you actually don't have, thus the linker errors.

    Do something similar like in the Qt sources (qglobal.h):
    @
    #if defined(MAKE_DLL)
    #define MY_DLL_EXPORT Q_DECL_EXPORT
    #else
    #define MY_DLL_EXPORT Q_DECL_IMPORT
    #endif
    @

    Then when building the dll you need to define MAKE_DLL with the compiler defines flag.

    And define the class like this instead:

    @
    class MY_DLL_EXPORT XEEL_ColorMixing_Effect : public QObject
    {
    Q_OBJECT
    ...
    }
    @



  • Thanks for the reply,
    I tried that but I got the following error from msvc:

    error C2491: 'BallsEffect::staticMetaObject' : definition of dllimport function not allowed G:\Dev\XEEL\trunk\XEELEffects\BallsEffect\BallsEffect-VS2010\GeneratedFiles\DebugDLL\moc_BallsEffect.cpp 80 BallsEffect

    I forgot to tell that I use Visual Studio 2010 to build my project on win8.1 x64 and Qt 5.2


  • Moderators

    something is still wrong in your code (which you have not posted yet) i guess.

    Please clean your project and rebuild it. If the error still occurs note the following:

    It's not allowed that your exported classes have inline implementations.
    Use the same header file in the dll and your application.



  • Oo didn't know about the inline implementations but I don't use them so it's not a problem.

    In one of my sdk headers I put:
    @
    #if defined(XEEL_SDK_MAKE_DLL)
    #define XEEL_SDK_DLL_EXPORT Q_DECL_EXPORT
    #else
    #define XEEL_SDK_DLL_EXPORT Q_DECL_IMPORT
    #endif
    @
    and use this macro in all the classes I must have access to.
    I use exactly the same headers in both dlls, and when I tried to compile the second dll I got a lot of warnings...

    I found my error, the final class, in this case BallEffect must not use the XEEL_SDK_DLL_EXPORT macro and it works fine :)

    thanks for your help ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.