Windows amp; C++: extern amp; __declspec(dllimport)(视窗C++: 外部 amp;__declspec(dllimport))
问题描述
extern"和__declspec(dllimport") 之间有什么区别/关系?我发现有时需要同时使用它们,有时一个就足够了.
What is the difference/relationship between "extern" and "__declspec(dllimport")? I found that sometimes it is necessary to use both of them, sometimes one is enough.
我说的对吗:
- extern"用于静态链接库,
- "__declspec(dllimport)" 用于 DLL(动态链接库),
- 对于各自的链接类型,两者实际上都做了相同的工作,
- 在使用导入库(有助于与 dll 链接的小 .lib 文件)时,您需要同时使用这两者吗?
推荐答案
extern
表示实体具有外部链接,即在其翻译单元(C 或 CPP 文件)之外可见.这意味着相应的符号将被放置在目标文件中,因此如果该目标文件成为静态库的一部分,它也将是可见的.但是,extern
本身并不意味着一旦目标文件成为 DLL 的一部分,该符号也将可见.
extern
means that the entity has external linkage, i.e. is visible outside its translation unit (C or CPP file). The implication of this is that a corresponding symbol will be placed in the object file, and it will hence also be visible if this object file is made part of a static library. However, extern
does not by itself imply that the symbol will also be visible once the object file is made part of a DLL.
__declspec(dllexport)
表示符号应该从 DLL 中导出(如果它确实是 DLL 的一部分).它在编译进入 DLL 的代码时使用.
__declspec(dllexport)
means that the symbol should be exported from a DLL (if it is indeed made part of a DLL). It is used when compiling the code that goes into the DLL.
__declspec(dllimport)
表示符号将从 DLL 中导入.在编译使用 DLL 的代码时使用它.
__declspec(dllimport)
means that the symbol will be imported from a DLL. It is used when compiling the code that uses the DLL.
因为在编译 DLL 本身以及将使用 DLL 的客户端代码时通常使用相同的头文件,所以习惯上定义一个解析为 __declspec(dllexport)
的宏编译 DLL 时和 __declspec(dllimport)
时编译其客户端,如下所示:
Because the same header file is usually used both when compiling the DLL itself as well as the client code that will use the DLL, it is customary to define a macro that resolves to __declspec(dllexport)
when compiling the DLL and __declspec(dllimport)
when compiling its client, like so:
#if COMPILING_THE_DLL
#define DLLEXTERN __declspec(dllexport)
#else
#define DLLEXTERN __declspec(dllimport)
#endif
回答您的具体问题:
- 是的,仅
extern
就足以用于静态库. - 是的 -- 声明还需要一个
extern
(看这里的解释). - 不完全——见上文.
- 您并不严格需要带有
__declspec(dllimport)
的extern
(请参阅上面链接的说明),但由于您通常会使用相同的标头extern
文件,因为在编译 DLL 时需要它.
- Yes,
extern
alone is sufficient for static libraries. - Yes -- and the declaration also needs an
extern
(see explanation here). - Not quite -- see above.
- You don't strictly need the
extern
with a__declspec(dllimport)
(see explanation linked to above), but since you'll usually be using the same header file, you'll already have theextern
in there because it's needed when compiling the DLL.
这篇关于视窗C++: 外部 &__declspec(dllimport)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:视窗C++: 外部 &__declspec(dllimport)
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01