dllimport和dllexport之间的链接 [英] linkage between dllimport and dllexport

查看:44
本文介绍了dllimport和dllexport之间的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对DLLexport,Windows中C ++中的dllimport有一些疑问.假设我有一些 module.cpp module.h ,它们使用 dllexport 导出函数.假设我也有 moduleWrapper.cpp moduleWrapper.h ,它们使用 dllimport module.cpp 导入函数.

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.

有人可以解释一下为什么我会错过在 moduleWrapper.cpp moduleWrapper.h 中编写 #include module.h 的原因.我不明白链接程序如何从module.cpp知道函数的地址,在此先感谢您的任何解释

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

推荐答案

据我了解,您在module.h中有类似的内容:

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

__declspec(dllexport) void f();

然后,在moduleWrapper.cpp中有一个类似的语句:

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

__declspec(dllimport) void f();

这被视为函数声明,因此您无需再包含module.h.通常的做法是将以下代码放入包含文件中:

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();

那样,当您编译DLL时,它将转换为dllexport,而在不属于DLL且碰巧正在使用标头的任何文件中,它将转换为dllimport.

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.

这篇关于dllimport和dllexport之间的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆