下面的声明/关键字背后的解释是什么?
我想知道以下声明的作用。 我在 MSVisual Studio 上的 C 代码中看到过它们编译的代码。
extern "C" __declspec(dllexport)
extern "C" __declspec(dllimport)
我知道一些它们用于声明函数的外部链接(在不同源文件中定义的函数。但想详细了解它是如何工作的。
-Ajit
I would like to know what the following declarations do. I have seen them in a C code on MSVisual Studio Compiled code.
extern "C" __declspec(dllexport)
extern "C" __declspec(dllimport)
I know somewhat that they are used to declare external linkage for functions(functional defined in different source file.But would like to know in detail how this works.
-Ajit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
extern "C"
部分告诉 C++ 编译器所声明的项应使用 C 链接,这意味着该名称不会被破坏(或者将以与 C 编译器相同的方式被破坏) )。 这使得该项目可以从 C 代码和大多数其他语言链接到,因为 C 链接通常是平台上使用的标准。__declspec(dllexport)
和__declspec(dllimport)
项是非标准属性,它们告诉编译器应从 DLL 导出(或导入)该项。 MS 编译器以及可能其他面向 Windows 的编译器支持__declspec()
属性。 我不确定 GCC 是否这样做。 可以使用 __declspec() 指定的其他存储类属性(至少在 MSVC 中)包括 uuid()、naked、deprecated 以及其他为编译器提供有关如何编译对象或函数的信息的内容。The
extern "C"
part tells a C++ compiler that the item being declared should use C linkage, which means that the name will not be mangled (or will be mangled in the same way that a C compiler would). This makes it so the item can be linked to from C code and most other languages as well, since C linkage is typically the standard used for that on a platform.The
__declspec(dllexport)
and__declspec(dllimport)
items are non-standard attributes that tell the compiler that the item should be exported (or imported) from a DLL. The__declspec()
attribute is supported on MS compilers and probably other compilers that target Windows. I'm not sure if GCC does or not. Other storage class attributes that can be specified with__declspec()
(at least in MSVC) includeuuid()
,naked
,deprecated
and others that provide the compiler with information on how an object or function should be compiled.dllexport 告诉编译器生成 .lib 文件。 dllimport 告诉编译器在 .lib 文件中查找函数声明(其定义将在 dll 中)。
dllexport tells the compiler to generate a .lib file. dllimport tells the compiler to look in a .lib file for the function declaration (its definition will be in a dll).
这意味着它后面的函数/类是可见的并且可以跨 DLL 边界访问,因此您可以链接它们并从其他代码调用它们
It means the functions/classes that follow it are visible and accessible across a DLL boundary so you can link against them and call them from other code