Search code examples
c++visual-studiodllvirtual

DLL Programming(C++ | MSVS) virtual and deriving


First off I'm pretty new to dll programming. So I'm programming an UI dll which should help me in my projects. The problem is that I'm getting errors, that don't really say much and I'm a little bit confused. So I've a class called UIContainer witch was supposed to be my base class from which all others(Buttons and so on) derive. And because I save them in a List of pointers for the type UIContainer I've to use virtual methodes. But when putting the virtual statement before my methodes it gave me errors so I researched an found out, that I've to declare them as pure virtual methodes so I created the UIElement class which simply is a Interface for the UIContainer to be able to use virtual methodes and don't have to reimplement them in every class that derives from UIContainer. But now I get these error and can't really explain why. Has anyone an idea?

Error 42    error LNK2001: Unresloved external symbol ""__declspec(dllimport) const Button::`vftable'" (__imp_??_7Button@@6B@)".

Error 43    error LNK2001: Unresloved external symbol ""__declspec(dllimport) const UIContainer::`vftable'" (__imp_??_7UIContainer@@6B@)".

Error 44    error LNK2001: Unresloved external symboll ""__declspec(dllimport) const UIElement::`vftable'" (__imp_??_7UIElement@@6B@)".

Error 45    error LNK1120: 3 unresloved externals

(Note that I tried to translate the errors in English, because I'm from germany and therefore get german errors;) )


Solution

  • So I found the Problem. If you're using virtual methodes in a dll you've to declare all methodes separate for im/export.

    To give an example:

    #ifdef UICONATINER_EXPORTS
    #define UICONATINER_API __declspec(dllexport) 
    #else
    #define UICONATINER_API __declspec(dllimport) 
    #endif
    

    Usually you would define the class as ex/import like this:

    class UICONATINER_API UIContainer
    ...
    

    But in case of virtual methodes that doesn't work and you've to do it like so for each methode:

    class UIContainer
    {
    ...
        UICONATINER_API void unfocused();
        UICONATINER_API void draw();
    ...
    }