What does __declspec (dllimport) mean?

I saw the Qt source code as follows:

class Q_CORE_EXPORT QBasicAtomicInt { public: ... }; 

Which macro Q_CORE_EXPORT is defined as follows:

 define Q_DECL_IMPORT __declspec(dllimport) 

So what does __declspec(dllimport) ?

+58
c ++ qt dll visual-c ++
Jan 14 '12 at 15:33
source share
3 answers

__declspec is a Microsoft-specific attribute that allows you to specify storage class information.
(Nitpicker Corner: However, a number of other compiler providers, such as GCC, now support this language extension for compatibility with an installed code base written for Microsoft compilers. Some even provide additional storage class attributes.) Sub>

Two of these storage class attributes that can be specified are dllimport and dllexport . They tell the compiler that the function or object is imported or exported (respectively) from the DLL.

In particular, they define a DLL interface for a client without requiring a module definition file ( .DEF ). Most people find it much easier to use these language extensions than creating DEF files.

For obvious reasons, __declspec(dllimport) and __declspec(dllexport) are usually paired with each other. You use dllexport to indicate the symbol exported from the DLL, and use dllimport to import the exported symbol into another file.

Because of this, and because the same header file is usually used both when compiling a DLL and in client code that consumes the DLL, this is a common template for defining a macro that automatically jumps to the corresponding attribute specifier when compilation time. For example:

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

Then check all the characters that should be exported using dllexport .

Presumably, this is what the Q_CORE_EXPORT macro Q_CORE_EXPORT , allowing either Q_DECL_IMPORT or Q_DECL_EXPORT .

+80
Jan 14 '12 at 15:38
source share

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

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

+8
Jan 14 '12 at 15:39
source share

This means that the function definition is in the dynamic library. See the documentation for more details and examples.

+1
Jan 14 '12 at 15:37
source share



All Articles