__declspec(dllimport)真的意思是什么? [英] what does __declspec(dllimport) really mean?

查看:331
本文介绍了__declspec(dllimport)真的意思是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  class Q_CORE_EXPORT QBasicAtomicInt 
{
public:
...
};

其中 Q_CORE_EXPORT 宏定义如下: / p>

  define Q_DECL_IMPORT __declspec(dllimport)

$ b $ __ declspec(dllimport)真的是什么意思?

解决方案

__ declspec 是一个Microsoft特定的属性,允许您指定存储类信息。

(Nitpicker's Corner:但是,许多其他编译器供应商 - 例如GCC-现在支持这种语言扩展,已经安装的代码基础是面向Microsoft的编译器,有些甚至提供额外的存储类属​​性。)

其中两个存储类属性可以指定 dllimport dllexport 。这些指示编译器函数或对象是从DLL导入或导出的。



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



显然, __ declspec(dllimport) __ declspec(dllexport)通常彼此配对。您可以使用 dllexport 将符号标记为从DLL导出,并使用 dllimport 在另一个文件。



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

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

然后标记所有符号



code>宏,解析为 Q_DECL_IMPORT Q_DECL_EXPORT


I saw the Qt source code like this:

class Q_CORE_EXPORT QBasicAtomicInt
{
public:
...
};

Which Q_CORE_EXPORT macro defines like below:

define Q_DECL_IMPORT __declspec(dllimport)

So what does __declspec(dllimport) really mean?

解决方案

__declspec is 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.)

Two of those storage-class attributes that can be specified are dllimport and dllexport. These indicate to the compiler that a function or object is imported or exported (respectively) from a 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.

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

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:

#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.

Presumably, that is what the Q_CORE_EXPORT macro does, resolving to either Q_DECL_IMPORT or Q_DECL_EXPORT.

这篇关于__declspec(dllimport)真的意思是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆