Windows & C++:extern & __declspec(dllimport)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2288293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:57:40  来源:igfitidea点击:

Windows & C++: extern & __declspec(dllimport)

c++windowsdlllinker

提问by liori

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”) 之间的区别/关系是什么?我发现有时候需要两个都用,有时候一个就够了。

Am I right that:

我说得对吗:

  1. "extern" is for statically linked libraries,
  2. "__declspec(dllimport)" is for DLL (dynamically linked libraries),
  3. both do actually the same job for their respective type of linking,
  4. you need to use both when you use import libraries (small .lib files that help linking with dll)?
  1. “extern”用于静态链接库,
  2. "__declspec(dllimport)" 用于 DLL(动态链接库),
  3. 两者实际上都为各自类型的链接做同样的工作,
  4. 当您使用导入库(有助于与 dll 链接的小型 .lib 文件)时,您需要同时使用两者吗?

回答by Martin B

externmeans 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, externdoes not by itself imply that the symbol will also be visible once the object file is made part of a DLL.

extern表示实体具有外部链接,即在其翻译单元(C 或 CPP 文件)之外可见。这意味着相应的符号将被放置在目标文件中,因此如果该目标文件成为静态库的一部分,它也将是可见的。但是,extern它本身并不意味着一旦目标文件成为 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(dllexport)意味着应该从 DLL 导出符号(如果它确实是 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.

__declspec(dllimport)意味着该符号将从 DLL 中导入。它在编译使用 DLL 的代码时使用。

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:

因为通常在编译 DLL 本身以及将使用 DLL 的客户端代码时使用相同的头文件,所以习惯上定义一个宏,__declspec(dllexport)在编译 DLL 和__declspec(dllimport)编译其客户端时解析为,如下所示:

#if COMPILING_THE_DLL
    #define DLLEXTERN __declspec(dllexport)
#else
    #define DLLEXTERN __declspec(dllimport)
#endif

To answer your specific questions:

要回答您的具体问题:

  1. Yes, externalone is sufficient for static libraries.
  2. Yes -- and the declaration also needs an extern(see explanation here).
  3. Not quite -- see above.
  4. You don't strictly need the externwith a __declspec(dllimport)(see explanation linked to above), but since you'll usually be using the same header file, you'll already have the externin there because it's needed when compiling the DLL.
  1. 是的,extern单独用于静态库就足够了。
  2. 是的 - 并且声明还需要一个extern请参阅此处的解释)。
  3. 不完全 - 见上文。
  4. 您并不严格需要externwith a __declspec(dllimport)(请参阅上面链接的解释),但是由于您通常会使用相同的头文件,因此您已经extern在那里拥有了,因为在编译 DLL 时需要它。