C++ __declspec(dllimport) 的真正含义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8863193/
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-08-28 19:14:46  来源:igfitidea点击:

what does __declspec(dllimport) really mean?

c++qtvisual-c++dlldeclspec

提问by gemfield

I saw the Qt source code like this:

我看到的Qt源代码是这样的:

class Q_CORE_EXPORT QBasicAtomicInt
{
public:
...
};

Which Q_CORE_EXPORTmacro defines like below:

哪个Q_CORE_EXPORT宏定义如下:

define Q_DECL_IMPORT __declspec(dllimport)

So what does __declspec(dllimport)really mean?

那么__declspec(dllimport)真正的意思是什么?

回答by Cody Gray

__declspecis a Microsoft-specific attribute that allows you to specify storage-class information.
(Nitpicker's Corner: However, a number of other compiler vendors—e.g. GCC—now support this language extension for compatibility with the installed base of code that was written targeting Microsoft's compilers. Some even provide additional storage-class attributes.)

__declspec是 Microsoft 特定的属性,允许您指定存储类信息。
(Nitpicker's Corner:但是,许多其他编译器供应商(例如 GCC)现在支持此语言扩展,以便与针对 Microsoft 编译器编写的已安装代码库兼容。有些甚至提供额外的存储类属​​性。)

Two of those storage-class attributes that can be specified are dllimportand dllexport. These indicate to the compiler that a function or object is imported or exported (respectively) from a DLL.

可以指定的两个存储类属性是dllimportdllexport。这些向编译器表明函数或对象是从 DLL 导入或导出(分别)。

More specifically, they define the DLL's interface to the client without requiring a module-definition (.DEF) file. Most people find it much easier to use these language extensions than to create DEF files.

更具体地说,它们定义了 DLL 到客户端的接口,而无需模块定义 ( .DEF) 文件。大多数人发现使用这些语言扩展比创建 DEF 文件要容易得多。

For obvious reasons, __declspec(dllimport)and __declspec(dllexport)are generally paired with one another. You use dllexportto mark a symbol as exported from a DLL, and you use dllimportto import that exported symbol in another file.

出于显而易见的原因,__declspec(dllimport)并且__declspec(dllexport)通常彼此配对。您用于dllexport将符号标记为从 DLL 导出,并dllimport用于将导出的符号导入另一个文件中。

Because of this, and because the same header file is generally used both when compiling the DLL and in client code that consumes the DLL's interface, it is a common pattern to define a macro that automatically resolves to the appropriate attribute specifier at compile-time. For example:

正因为如此,并且因为在编译 DLL 时和在使用 DLL 接口的客户端代码中通常使用相同的头文件,定义一个在编译时自动解析为适当的属性说明符的宏是一种常见的模式。例如:

#if COMPILING_DLL
    #define DLLEXPORT __declspec(dllexport)
#else
    #define DLLEXPORT __declspec(dllimport)
#endif

And then marking all of the symbols that should be exported with DLLEXPORT.

然后标记所有应该导出的符号DLLEXPORT

Presumably, that is what the Q_CORE_EXPORTmacro does, resolving to either Q_DECL_IMPORTor Q_DECL_EXPORT.

据推测,这就是Q_CORE_EXPORT宏所做的,解析为Q_DECL_IMPORTQ_DECL_EXPORT

回答by arx

__declspec(dllimport)is a storage-class specifier that tells the compiler that a function or object or data type is defined in an external DLL.

__declspec(dllimport)是一个存储类说明符,它告诉编译器一个函数、对象或数据类型是在外部 DLL 中定义的。

The function or object or data type is exported from a DLL with a corresponding __declspec(dllexport).

函数或对象或数据类型从 DLL 中导出,并带有相应的__declspec(dllexport).

回答by Lewis Kelsey

__declspec(dllexport)tells the compiler to inform the linker that these symbols need to be placed in the export table (when compiling the .dll). When compiling the program that links with the .dll, __declspec(dllimport)tells the compiler to produce a rip-relative absolute register-indirectindirect call (which the linker will fill resolve to point to the import table) rather than the usual rip-relative register-directindirect call instruction to an undefined function (which, as it can't modify the instruction, the linker inserts the relative address of a thunk and then creates the thunk, inside which it places the rip-relative absolute register-indirect indirect call to the function pointer in the import table). This is a code size and speed optimisation. It is the import library .lib that tells the linker which symbols will be imported and is used as a guide to create the import table and create any necessary thunks in the .text segment.

__declspec(dllexport)告诉编译器通知链接器这些符号需要放在导出表中(在编译 .dll 时)。在编译与 .dll 链接的程序时,__declspec(dllimport)告诉编译器生成一个 rip 相对绝对寄存器间接调用(链接器将填充解析为指向导入表),而不是通常的 rip 相对寄存器直接调用对未定义函数的间接调用指令(由于它不能修改指令,因此链接器插入一个 thunk 的相对地址,然后创建 thunk,在其中放置 rip 相对绝对寄存器-间接间接调用到导入表中的函数指针)。这是代码大小和速度优化。导入库 .lib 告诉链接器将导入哪些符号,并用作创建导入表和在 .text 段中创建任何必要 thunk 的指南。

https://docs.microsoft.com/en-us/cpp/build/importing-function-calls-using-declspec-dllimport?view=vs-2019https://docs.microsoft.com/en-us/cpp/build/importing-data-using-declspec-dllimport?view=vs-2019https://stackoverflow.com/a/4490536/7194773

https://docs.microsoft.com/en-us/cpp/build/importing-function-calls-using-declspec-dllimport?view=vs-2019 https://docs.microsoft.com/en-us/cpp /build/importing-data-using-declspec-dllimport?view=vs-2019 https://stackoverflow.com/a/4490536/7194773

回答by Armen Tsirunyan

It means that the definition of the function is in a dynamic library. Refer to the documentationfor more details and examples.

这意味着函数的定义在动态库中。有关更多详细信息和示例,请参阅文档