Export DLLs classes and functions and import them into Win32 Application

1k Views Asked by At

I have a dll with a class that define some methods and variables inside it. I marked it as

__declspec(dllexport)

and i imported the .h header inside a win32 application project in the same solution. I can use the functions but when I try to compile the project I have a lot of errors about external symbols not resolved. Why?

2

There are 2 best solutions below

0
On BEST ANSWER

Please read about the standard way of using macros for this very common task here: http://wiki.tcl.tk/8721

The basic idea is that you define a macro, say MY_API like so:

    #ifdef BUILD_MYAPI
    #    define MY_API __declspec(dllexport)
    #else
    #    define MY_API __declspec(dllimport)
    #endif

When you declare a function or a class in the header file you do this:

void MY_API myApiFunction(int x);

When you build your own dll which declares the body of the function, you add the definition of BUILD_MYAPI to the build. This makes all declerations to be dllexport
when you include the header from some other dll you don't add BUILD_MYAPI so the decelerations are dllimport
When compiling with visual studio, you can add a macro definition to the compilation without changing the source from project properties -> C/C++ -> Preprocesson -> Preprocessor definitions

2
On

For the application where you want to import that class, you will need to mark the class as

__declspec(dllimport)

Instead of dllexport.

You must also make sure to link with the import library of the DLL (a .lib file).