問題描述
我看到的Qt源代碼是這樣的:
I saw the Qt source code like this:
class Q_CORE_EXPORT QBasicAtomicInt
{
public:
...
};
Q_CORE_EXPORT
宏定義如下:
define Q_DECL_IMPORT __declspec(dllimport)
那么 __declspec(dllimport)
到底是什么意思?
So what does __declspec(dllimport)
really mean?
推薦答案
__declspec
是 Microsoft 特定的屬性,允許您指定存儲(chǔ)類信息.
(Nitpicker's Corner:但是,許多其他編譯器供應(yīng)商(例如 GCC)現(xiàn)在支持此語言擴(kuò)展,以便與針對(duì) Microsoft 編譯器編寫的已安裝代碼庫兼容.有些甚至提供額外的存儲(chǔ)類屬??性.)
可以指定的兩個(gè)存儲(chǔ)類屬性是 dllimport
和 dllexport
.這些向編譯器表明函數(shù)或?qū)ο笫菑?DLL 導(dǎo)入或?qū)С?分別).
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.
更具體地說,它們定義了 DLL 到客戶端的接口,而無需模塊定義 (.DEF
) 文件.大多數(shù)人發(fā)現(xiàn)使用這些語言擴(kuò)展比創(chuàng)建 DEF 文件要容易得多.
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.
出于顯而易見的原因,__declspec(dllimport)
和 __declspec(dllexport)
通常是相互配對(duì)的.您使用 dllexport
將符號(hào)標(biāo)記為從 DLL 導(dǎo)出,然后使用 dllimport
將該導(dǎo)出的符號(hào)導(dǎo)入另一個(gè)文件.
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.
因此,并且因?yàn)樵诰幾g DLL 時(shí)和在使用 DLL 接口的客戶端代碼中通常使用相同的頭文件,定義一個(gè)在編譯時(shí)自動(dòng)解析為適當(dāng)屬性說明符的宏是一種常見模式-時(shí)間.例如:
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
然后用DLLEXPORT
標(biāo)記所有應(yīng)該導(dǎo)出的符號(hào).
And then marking all of the symbols that should be exported with DLLEXPORT
.
據(jù)推測(cè),這就是 Q_CORE_EXPORT
宏所做的,解析為 Q_DECL_IMPORT
或 Q_DECL_EXPORT
.
Presumably, that is what the Q_CORE_EXPORT
macro does, resolving to either Q_DECL_IMPORT
or Q_DECL_EXPORT
.
這篇關(guān)于__declspec(dllimport) 的真正含義是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!