Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linkage between dllimport and dllexport

Tags:

c++

windows

dll

I have some question concerning dllexport, dllimport in C++ in Windows. Let's assume I have some module.cpp and module.h which export functions using dllexport. Let's assume that I also have moduleWrapper.cpp and moduleWrapper.h which imports functions from module.cpp using dllimport.

Can somebody please explain why can I miss writing #include module.h in my moduleWrapper.cpp and moduleWrapper.h. I can't understand how linker does know about addresses of functions from module.cpp, thanks in advance for any explanation

like image 273
geek Avatar asked Aug 07 '11 20:08

geek


1 Answers

From what I understand, you have something like this in module.h:

__declspec(dllexport) void f();

And then, you have a similar statement in your moduleWrapper.cpp:

__declspec(dllimport) void f();

That counts as a function declaration, so you remove any need to include module.h. The way it's commonly done is by putting the following code in your include file:

#ifdef PROJECTNAME_EXPORTS // (the default macro in Visual Studio)
#define PROJECTAPI __declspec(dllexport)
#else
#define PROJECTAPI __declspec(dllimport)
#endif

And then declaring your function in the header file like this:

PROJECTAPI void f();

That way, it will translate to dllexport when you're compiling your DLL, and to dllimport in any files that are not part of your DLL and that happen to be using the header.

like image 107
Amphetaman Avatar answered Oct 03 '22 08:10

Amphetaman