<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[vererbte Klasse aus dll exportieren]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich habe eine dll, die die Klasse &quot;System&quot; durch das &quot;SystemInterface&quot; exportiert.<br />
Soweit funktioniert das Beispiel.</p>
<p>Jetzt will ich aber nicht nur eine einfache Klasse:</p>
<pre><code>class SYSTEMSHARED_EXPORT System : public SystemInterface 
...
</code></pre>
<p>sondern eine Klasse, die von einem &quot;SystemWrapper&quot; beerbt wird also:</p>
<pre><code>class SYSTEMSHARED_EXPORT System : public SystemInterface, public SystemWrapper
...
</code></pre>
<p>wobei &quot;SystemWrapper&quot; schon ein paar - nicht virtuelle - Methoden definiert. (z.B. resetTime in diesem Testbeispiel)<br />
Bei Linken bekomme ich &quot;undefined reference to SystemWrapper::SystemWrapper()</p>
<p>Ich weiss, dass ich mich besser in c++ Basics einlesen muesste weil das Problem wahrscheinlich leicht loesbar ist, aber ich komme einfach nicht drauf.</p>
<p>Kann mir jemand helfen?<br />
Danke</p>
<p>system.h</p>
<pre><code>#ifndef SYSTEM_H
#define SYSTEM_H

#include &quot;system_global.h&quot;
#include &quot;systemInterface.h&quot;
#include &quot;../Simulator/systemwrapper.h&quot;
#include &lt;iostream&gt;

class SYSTEMSHARED_EXPORT System : public SystemInterface, public SystemWrapper
{

public:
    System(int startTime)
        : SystemWrapper(),
          t_(startTime)
    {
        std::cout &lt;&lt; &quot;System construct\n&quot;;
    }

    ~System()
    {
        std::cout &lt;&lt; &quot;System destruct\n&quot;;
    }

    void destroy();
    int setTime(int t);

    int t_;
};

extern &quot;C&quot; __declspec(dllexport) SystemInterface* __cdecl createSystem(int startTime=0)
{
    return new System(startTime);
}

#endif // SYSTEM_H
</code></pre>
<p>system.cpp</p>
<pre><code>#include &quot;system.h&quot;

void System::destroy() {
    delete this;
}

int System::setTime(int t){
    t_=t;
    std::cout &lt;&lt; &quot;time set: &quot; &lt;&lt; t_ &lt;&lt; &quot;\n&quot;;
    return t_;
}
</code></pre>
<p>system_golbal.h</p>
<pre><code>#ifndef SYSTEM_GLOBAL_H
#define SYSTEM_GLOBAL_H

#include &lt;QtCore/qglobal.h&gt;

#if defined(SYSTEM_LIBRARY)
#  define SYSTEMSHARED_EXPORT Q_DECL_EXPORT
#else
#  define SYSTEMSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // SYSTEM_GLOBAL_H
</code></pre>
<p>systemInterface.h</p>
<pre><code>#ifndef SYSTEMINTERFACE_H
#define SYSTEMINTERFACE_H

class SystemInterface {
public:
    virtual void destroy()=0;
    virtual int setTime(int t)=0;
};

#endif // SYSTEMINTERFACE_H
</code></pre>
<p>systemWrapper.h</p>
<pre><code>#ifndef SYSTEMWRAPPER_H
#define SYSTEMWRAPPER_H

class SystemWrapper
{
public:
    SystemWrapper();
    void resetTime();
    virtual int setTime(int t)=0;

    int t_;
};

#endif // SYSTEMWRAPPER_H
</code></pre>
<p>systemWrapper.cpp</p>
<pre><code>#include &quot;systemwrapper.h&quot;

SystemWrapper::SystemWrapper()
{
}

void SystemWrapper::resetTime() {
    set t_=0;
}
</code></pre>
<p>main.cpp</p>
<pre><code>#include &quot;../System/systemInterface.h&quot;
#include &lt;iostream&gt;
#include &lt;windows.h&gt;
using namespace std;

typedef SystemInterface* (__cdecl *system_factory)();

int main()
{
    HINSTANCE dll_handle = ::LoadLibrary(TEXT(&quot;../build-System/debug/System.dll&quot;));
    if (!dll_handle) {
        cerr &lt;&lt; &quot;Unable to load DLL!\n&quot;;
        return 1;
    }

    system_factory factory_func = reinterpret_cast&lt;system_factory&gt;(
        ::GetProcAddress(dll_handle, &quot;createSystem&quot;));
    if (!factory_func) {
        cerr &lt;&lt; &quot;Unable to load createSystem from DLL!\n&quot;;
        ::FreeLibrary(dll_handle);
        return 1;
    }

    SystemInterface* instance = factory_func();

    int t = instance-&gt;setTime(120);
    cout &lt;&lt; &quot;t = &quot; &lt;&lt; t &lt;&lt; endl;

    instance-&gt;destroy();
    ::FreeLibrary(dll_handle);

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/325424/vererbte-klasse-aus-dll-exportieren</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Jul 2026 20:51:23 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325424.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 01 May 2014 11:37:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to vererbte Klasse aus dll exportieren on Thu, 01 May 2014 11:37:19 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich habe eine dll, die die Klasse &quot;System&quot; durch das &quot;SystemInterface&quot; exportiert.<br />
Soweit funktioniert das Beispiel.</p>
<p>Jetzt will ich aber nicht nur eine einfache Klasse:</p>
<pre><code>class SYSTEMSHARED_EXPORT System : public SystemInterface 
...
</code></pre>
<p>sondern eine Klasse, die von einem &quot;SystemWrapper&quot; beerbt wird also:</p>
<pre><code>class SYSTEMSHARED_EXPORT System : public SystemInterface, public SystemWrapper
...
</code></pre>
<p>wobei &quot;SystemWrapper&quot; schon ein paar - nicht virtuelle - Methoden definiert. (z.B. resetTime in diesem Testbeispiel)<br />
Bei Linken bekomme ich &quot;undefined reference to SystemWrapper::SystemWrapper()</p>
<p>Ich weiss, dass ich mich besser in c++ Basics einlesen muesste weil das Problem wahrscheinlich leicht loesbar ist, aber ich komme einfach nicht drauf.</p>
<p>Kann mir jemand helfen?<br />
Danke</p>
<p>system.h</p>
<pre><code>#ifndef SYSTEM_H
#define SYSTEM_H

#include &quot;system_global.h&quot;
#include &quot;systemInterface.h&quot;
#include &quot;../Simulator/systemwrapper.h&quot;
#include &lt;iostream&gt;

class SYSTEMSHARED_EXPORT System : public SystemInterface, public SystemWrapper
{

public:
    System(int startTime)
        : SystemWrapper(),
          t_(startTime)
    {
        std::cout &lt;&lt; &quot;System construct\n&quot;;
    }

    ~System()
    {
        std::cout &lt;&lt; &quot;System destruct\n&quot;;
    }

    void destroy();
    int setTime(int t);

    int t_;
};

extern &quot;C&quot; __declspec(dllexport) SystemInterface* __cdecl createSystem(int startTime=0)
{
    return new System(startTime);
}

#endif // SYSTEM_H
</code></pre>
<p>system.cpp</p>
<pre><code>#include &quot;system.h&quot;

void System::destroy() {
    delete this;
}

int System::setTime(int t){
    t_=t;
    std::cout &lt;&lt; &quot;time set: &quot; &lt;&lt; t_ &lt;&lt; &quot;\n&quot;;
    return t_;
}
</code></pre>
<p>system_golbal.h</p>
<pre><code>#ifndef SYSTEM_GLOBAL_H
#define SYSTEM_GLOBAL_H

#include &lt;QtCore/qglobal.h&gt;

#if defined(SYSTEM_LIBRARY)
#  define SYSTEMSHARED_EXPORT Q_DECL_EXPORT
#else
#  define SYSTEMSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // SYSTEM_GLOBAL_H
</code></pre>
<p>systemInterface.h</p>
<pre><code>#ifndef SYSTEMINTERFACE_H
#define SYSTEMINTERFACE_H

class SystemInterface {
public:
    virtual void destroy()=0;
    virtual int setTime(int t)=0;
};

#endif // SYSTEMINTERFACE_H
</code></pre>
<p>systemWrapper.h</p>
<pre><code>#ifndef SYSTEMWRAPPER_H
#define SYSTEMWRAPPER_H

class SystemWrapper
{
public:
    SystemWrapper();
    void resetTime();
    virtual int setTime(int t)=0;

    int t_;
};

#endif // SYSTEMWRAPPER_H
</code></pre>
<p>systemWrapper.cpp</p>
<pre><code>#include &quot;systemwrapper.h&quot;

SystemWrapper::SystemWrapper()
{
}

void SystemWrapper::resetTime() {
    set t_=0;
}
</code></pre>
<p>main.cpp</p>
<pre><code>#include &quot;../System/systemInterface.h&quot;
#include &lt;iostream&gt;
#include &lt;windows.h&gt;
using namespace std;

typedef SystemInterface* (__cdecl *system_factory)();

int main()
{
    HINSTANCE dll_handle = ::LoadLibrary(TEXT(&quot;../build-System/debug/System.dll&quot;));
    if (!dll_handle) {
        cerr &lt;&lt; &quot;Unable to load DLL!\n&quot;;
        return 1;
    }

    system_factory factory_func = reinterpret_cast&lt;system_factory&gt;(
        ::GetProcAddress(dll_handle, &quot;createSystem&quot;));
    if (!factory_func) {
        cerr &lt;&lt; &quot;Unable to load createSystem from DLL!\n&quot;;
        ::FreeLibrary(dll_handle);
        return 1;
    }

    SystemInterface* instance = factory_func();

    int t = instance-&gt;setTime(120);
    cout &lt;&lt; &quot;t = &quot; &lt;&lt; t &lt;&lt; endl;

    instance-&gt;destroy();
    ::FreeLibrary(dll_handle);

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2396961</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2396961</guid><dc:creator><![CDATA[kwurpi]]></dc:creator><pubDate>Thu, 01 May 2014 11:37:19 GMT</pubDate></item><item><title><![CDATA[Reply to vererbte Klasse aus dll exportieren on Thu, 01 May 2014 12:32:14 GMT]]></title><description><![CDATA[<p>Hast du vllt. einfach vergessen, die SystemWrapper bei deinem Projekt mitzulinken?<br />
Welche IDE verwendest du denn (VS, QtCreator, ...)?</p>
<p>Mir erscheint auch dein Klassendesign etwas eigenartig. Wäre es nicht besser, wenn SystemWrapper von SystemInterface abgeleitet wäre, so daß dann System nur direkt von SystemWrapper erbt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2396969</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2396969</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Thu, 01 May 2014 12:32:14 GMT</pubDate></item><item><title><![CDATA[Reply to vererbte Klasse aus dll exportieren on Thu, 01 May 2014 12:44:06 GMT]]></title><description><![CDATA[<p>Ich verwende Qt-Creator.<br />
Du hast Recht, ich werde System nur SystemWrapper erben lassen.<br />
Kannst du mir sagen wie ich in Qt-Creator dem Linker SystemWrapper mitgebe?<br />
In jeden Fall, Danke fuer deine Hilfe. Ich weiss jetzt schon mehr wie vorher.</p>
<p><a href="http://System.pro" rel="nofollow">System.pro</a></p>
<pre><code>QT       -= gui

TARGET = System
TEMPLATE = lib

DEFINES += SYSTEM_LIBRARY

SOURCES += system.cpp

HEADERS += system.h\
        system_global.h \
        systemInterface.h

unix {
    target.path = /usr/lib
    INSTALLS += target
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2396972</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2396972</guid><dc:creator><![CDATA[kwurpi]]></dc:creator><pubDate>Thu, 01 May 2014 12:44:06 GMT</pubDate></item><item><title><![CDATA[Reply to vererbte Klasse aus dll exportieren on Thu, 01 May 2014 13:11:31 GMT]]></title><description><![CDATA[<p>Du hast die Sourcen noch nicht mal kompiliert:</p>
<pre><code>SOURCES += system.cpp \
           systemWrapper.cpp
</code></pre>
<p>Und die main.cpp fehlt da ja auch noch, oder bindet man die im QtCreator anders ein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2396979</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2396979</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Thu, 01 May 2014 13:11:31 GMT</pubDate></item><item><title><![CDATA[Reply to vererbte Klasse aus dll exportieren on Thu, 01 May 2014 13:27:34 GMT]]></title><description><![CDATA[<p>Nein, das ist das .pro file fuer die dll.<br />
Das main.cpp steht im .pro file vom eigentlichen Projekt.</p>
<p>Egal, ich hab's inzwischen so gemacht wie du vorgeschlagen hast, bzw. SystemWrapper einfach mit SystemInterface verheiratet,<br />
sodass es nur noch eine einfache Vererbung gibt.<br />
Und schon laueft's!</p>
<p>Vielen Dank nochmal.<br />
kwurpi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2396981</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2396981</guid><dc:creator><![CDATA[kwurpi]]></dc:creator><pubDate>Thu, 01 May 2014 13:27:34 GMT</pubDate></item></channel></rss>