Windows& C ++:extern& __declspec(dllimport) [英] Windows & C++: extern & __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" is for statically linked libraries,
- "__declspec(dllimport)" is for DLL (dynamically linked libraries),
- both do actually the same job for their respective type of linking,
- you need to use both when you use import libraries (small .lib files that help linking with dll)?
推荐答案
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,通常在编译DLL和 __ declspec(dllimport)$ c $时定义一个解析为
__ declspec(dllexport)
c>当编译其客户端时,像这样:
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.
这篇关于Windows& C ++:extern& __declspec(dllimport)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!