Gangrel
Gangrel

Reputation: 157

Export C++ class from main program to DLL

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:

And the source code files are:

class ON_MAIN tst_class {
public:
     tst_class(char *);
    ~tst_class(void);
};
#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");
}
#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;
}
#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:

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...

What I'm doing wrong with compiling/linking the library?

The executable works well. Not loading the library of cource, because it not exists.

Upvotes: 1

Views: 115

Answers (1)

Niall
Niall

Reputation: 30624

Your defines are not quite right. The form of the declspec is generally

#if BUILDING_MYDLL
  #define MYDLL_EXPORT  __declspec(dllexport)
#else
  #define MYDLL_EXPORT  __declspec(dllimport)
#endif

And for the definition of the class

class MYDLL_EXPORT tst_class {
public:
     tst_class(char *);
    ~tst_class(void);
};

Then when building and linking the dll code, you use a command line define for BUILDING_MYDLL (e.g. -D BUILDING_MYDLL) or you define BUILDING_MYDLL in a header file that is internal to the dll (e.g. in Visual Studio based solutions that was often stdafx.h).

Here is another answer that is related.

Upvotes: 1

Related Questions