linkage between dllimport and dllexport
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 addr开发者_运维技巧esses of functions from module.cpp, thanks in advance for any explanation
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.
精彩评论