Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does __declspec(dllimport) really mean?

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?

like image 780
gemfield Avatar asked Jan 14 '12 15:01

gemfield


People also ask

Is __ Declspec Dllimport necessary?

Using __declspec(dllimport) is optional on function declarations, but the compiler produces more efficient code if you use this keyword. However, you must use __declspec(dllimport) for the importing executable to access the DLL's public data symbols and objects.

What does Dllimport mean?

DllImport Attribute is a declarative tag used in C# to mark a class method as being defined in an external dynamic-link library (DLL) rather than in any . NET assembly.

What is __ Declspec in C++?

__declspecThe extended attribute syntax simplifies and standardizes Microsoft-specific extensions to the C and C++ languages.

What does __ Declspec Dllexport do?

__declspec(dllexport) adds the export directive to the object file so you do not need to use a . def file. This convenience is most apparent when trying to export decorated C++ function names.


2 Answers

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

like image 150
Cody Gray Avatar answered Sep 23 '22 02:09

Cody Gray


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

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

like image 45
arx Avatar answered Sep 22 '22 02:09

arx