dll Erzeugung schlägt fehl



  • Hallo zusammen,

    habe mich an die dll Erzeugung herangewagt und mir ein einfaches Beispiel nach Anleitung ausgesucht. Ich bekomme aber jedesmal beim Kompilieren folgende Fehlermeldung:
    [Linker Error] Unresolved external 'TForm1::Addition(int, int)' referenced from D:\BORLAND\CBUILDER6\PROJECTS\UNIT1.OBJ

    Mein Code sieht dabei folgendermaßen aus:

    dllunit.cpp

    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #include <windows.h>
    #pragma hdrstop
    //---------------------------------------------------------------------------
    //   Important note about DLL memory management when your DLL uses the
    //   static version of the RunTime Library:
    //
    //   If your DLL exports any functions that pass String objects (or structs/
    //   classes containing nested Strings) as parameter or function results,
    //   you will need to add the library MEMMGR.LIB to both the DLL project and
    //   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
    //   if any other projects which use the DLL will be performing new or delete
    //   operations on any non-TObject-derived classes which are exported from the
    //   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
    //   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
    //   the file BORLNDMM.DLL should be deployed along with your DLL.
    //
    //   To avoid using BORLNDMM.DLL, pass string information using "char *" or
    //   ShortString parameters.
    //
    //   If your DLL uses the dynamic version of the RTL, you do not need to
    //   explicitly add MEMMGR.LIB as this will be done implicitly for you
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
    {
            return 1;
    }
    //---------------------------------------------------------------------------
    
    int __export Addition(int z1,int z2)
    {
     return z1 + z2;
    }
    

    Die Projekt Unit, in der die dll aufgerufen und verwendet wird
    Unit1.h

    //---------------------------------------------------------------------------
    
    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:	// IDE-managed Components
            TEdit *Edit1;
            TEdit *Edit2;
            TEdit *Edit3;
            TButton *Button1;
            void __fastcall Button1Click(TObject *Sender);
    private:	// User declarations
    public:		// User declarations
            __fastcall TForm1(TComponent* Owner);
            int __import Addition(int, int);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
    

    Unit1.cpp

    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    int a=StrToInt(Edit1->Text);
    int b=StrToInt(Edit2->Text);
    Edit3->Text=Addition(a,b);
    }
    //---------------------------------------------------------------------------
    

    Habe die dll Unit kompiliert und erzeugt. Habe folgende Dateien bekommen:

    addition.dll
    addition.lib
    ...

    Habe dann dem Projekt über Project->Add to Project die Datei addition.lib hinzugefügt und das ganze Kompiliert und wie oben genannt die Fehlermeldung bekommen.

    Woran kann das liegen?

    Danke
    Todd



  • Habe jetzt eine andere Variante versucht und es klappt zumindest:

    dllUnit.cpp

    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #include <windows.h>
    #pragma hdrstop
    //---------------------------------------------------------------------------
    //   Important note about DLL memory management when your DLL uses the
    //   static version of the RunTime Library:
    //   ....
    //   If your DLL uses the dynamic version of the RTL, you do not need to
    //   explicitly add MEMMGR.LIB as this will be done implicitly for you
    //---------------------------------------------------------------------------
    extern "C" __declspec(dllexport) int Min(int, int);
    #pragma argsused
    int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
    {
            return 1;
    }
    //---------------------------------------------------------------------------
    
    int Min(int z1,int z2)
    {
     if (z1>z2)
     return z2;
     else
     return z1;
    }
    

    ProjectUnit.cpp

    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "MainUnit.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    extern "C" __declspec(dllimport) int Min(int, int);
    
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    Edit1->Text=Min(7,2);
    }
    

    Die Header Datei wurde dabei unverändert gelassen...

    Wie gesagt so funkioniert es, mich würde trotzdem interesseiren, warum die erste Variante nicht funktioniert.
    Vielleicht hat ja jemand von euch eine Idee...

    Danke und Liebe Grüße
    Todd



  • Genau die gleiche Fehlermeldung habe ich bei meinem Programm, jetzt werd ich mal deine Variante versuchen ^^

    Wäre allerdings wirklich interessant was genau diesen Fehler hervorruft 😉

    //edit

    Bei mir ändert das nichts, ich bekomme immer noch diese Fehlermeldung von wegen "Unresolved external ...".

    Ich arbeite gerade mit dem Profibus (wenn das jemanden hilft) von Siemens - zwar schon etwas älter aber so lautet halt mein Auftrag.

    Ich soll eine DLL erstellen um den Profibus anzusteuern und bestimmte Daten aus einem Datenfile zu verarbeiten. Die Funktionen für die Ansteuerung hab ich schon fertig, soll sie aber wie gesagt nun in eine DLL verpacken, damit sie nachher leichter verwendet werden kann.
    In meinen Funktionen verwende ich 5 von Siemens vorgegebene Funktionen (DP_start_cp, DP_open, DP_get_err_txt, DP_get_pointer und DP_set_mode). Jedoch bekomme ich beim erstellen meiner DLL immer die Fehlermeldung mit "Unresolved external <func> referenced from "C:\..\profibus.obj" und das nur bei den 5 Funktionen von Siemens.
    Die Funktionen sind allerdings in der DP_BASE.dll vorhanden und auch in der DP_BASE.LIB, welche ich in mein Projekt eingebunden habe.
    Weiß da vielleicht irgendjemand Rat?



  • Tritt der Fehler auch auf, wenn du jede einzelne Funktion exportierst, d.h. wenn dein dll.cpp folgendermaßen aufgebaut ist:

    dll.cpp

    //---------------------------------------------------------------------------
    #include <vcl.h>
    #include <windows.h>
    #pragma hdrstop
    //---------------------------------------------------------------------------
    //   Important note about DLL memory management when your DLL uses the
    //   static version of the RunTime Library:
    //   ....
    //   If your DLL uses the dynamic version of the RTL, you do not need to
    //   explicitly add MEMMGR.LIB as this will be done implicitly for you
    //---------------------------------------------------------------------------
    extern "C" __declspec(dllexport) typ func1(...)
    extern "C" __declspec(dllexport) typ func2(...)
    //usw...
    #pragma argsused
    int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
    {
            return 1;
    }
    //---------------------------------------------------------------------------
    
    typ func1(...)
    {...}
    
    typ func2(...)
    {...}
    
    //usw...
    

    Die Einbindung muss natürlich dann auch für jede Funktion extra geschehn:

    MainUnit.cpp

    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "MainUnit.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    extern "C" __declspec(dllimport) typ func1(...);
    extern "C" __declspec(dllimport) typ func2(...);
    //usw...
    
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    

    Vielleicht war es das ja schon.



  • Nein, so funktioniert es auch nicht.

    Inzwischen hab ich in den Borland C++ Builder FAQs etwas zu Thema DLLs einbinden gefunden. Damit funktioniert es jetzt auch.

    Danke trotzdem für deine Hilfe 🙂

    Falls du Interesse hast hier der Link: http://www.c-plusplus.net/forum/viewtopic-var-t-is-39265.html



  • Jab, den Link hab ich mir schon durchgelesen. Finde ich nur etwas umständlich.
    Mache das jetzt nicht wie ich oben gepostet hab, statisch, sondern benutze die dynamische Einbindung der DLL's...



  • Gibts dazu einen Link oder sowas? Wenn, dann poste ihn bitte 🙂



  • Hallo

    Alles wichtige zu DLLs im Builder wird hier in unserem Tutorial erklärt.

    bis bald
    akari



  • Herzlichen Dank akari!


Anmelden zum Antworten