With C++, i'm trying to use a class declared on my main program instance to be used with on DLL. It's a lot of examples to a class from DLL to use on main program, but I need the other way. For now, a simple declaration is ok, but my next step will be use a static class from main program on DLL.
For knowledge, I'm using MinGW 3.4.5 on Windows 10.
For an example I'm trying to generate 3 files:
- tst_class.lib
- tst_library.dll
- tst.exe
And the source code files are:
- tst_class.h
class ON_MAIN tst_class {
public:
     tst_class(char *);
    ~tst_class(void);
};
- tst_class.c
#include <stdio.h>
#define ON_MAIN  __declspec(dllexport)
#define ON_DLL   __declspec(dllimport)
#include "tst_class.h"
tst_class::tst_class(char *source)
{
    printf("new tst_class() from %s\n", source);
}
tst_class::~tst_class(void)
{
    printf("delete (tst_class *)\n");
}
- tst_library.c
#include <windows.h>
#define ON_MAIN  __declspec(dllimport)
#define ON_DLL   __declspec(dllexport)
#include "tst_class.h"
ON_DLL void tst(void)
{
    tst_class *t = new tst_class("library");
    delete t;
}
- tst.c
#include <stdio.h>
#include <windows.h>
#define ON_MAIN  __declspec(dllexport)
#define ON_DLL   __declspec(dllimport)
#include "tst_class.h"
typedef void (__cdecl *f_pointer)(void);
int main(int argc, char **argv)
{
    tst_class *t = new tst_class("main");
    delete t;
    HMODULE module = LoadLibrary("tst_library.dll");
    if(module != NULL) {
        printf("Dll loaded\n");
        void *function_pointer = (void *)GetProcAddress(module, "tst");
        if(function_pointer != NULL) {
            f_pointer function = (f_pointer)function_pointer;
            function();
        }
        FreeLibrary(module);
    } else {
        printf("Can't load dll\n");
    }
    return 0;
}
To compile them:
- g++ -c tst_class.c -o tst_class.oOK
- ar cr tst_class.lib tst_class.oOK
- g++ tst_library.c tst_class.lib -o tst_library.dll -sharedError
And here I've got this error message:
.../ccpg0mO9.o:tst_library.c:(.text+0x5a): undefined reference to '_imp___ZN9tst_classC1EPc' .../ccpg0mO9.o:tst_library.c:(.text+0xb4): undefined reference to '_imp___ZN9tst_classD1Ev'
But following to the last step...
- g++ tst.c tst_class.lib -o tst.exeOK
What I'm doing wrong with compiling/linking the library?
The executable works well. Not loading the library of cource, because it not exists.
 
     
    