Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why/when is __declspec( dllimport ) not needed?

In a project using a server.dll and a client.exe, I have dllexported a server symbol from the server dll, and not dllimported it into the client exe.

Still, the application links, and starts, without any problem. Is dllimport not needed, then???

Details:

I have this 'server' dll:

// server.h #ifdef SERVER_EXPORTS   #define SERVER_API __declspec(dllexport) #else   #define SERVER_API // =====> not using dllimport! #endif class  SERVER_API CServer {    static long s;    public:    CServer(); };  // server.cpp CServer::CServer(){}  long CServer::s; 

and this client executable:

#include <server.h> int main() {    CServer s; } 

The server command line:

cl.exe /Od  /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_USRDLL"   /D "SERVER_EXPORTS" /D "_UNICODE" /D "UNICODE" /D "_WINDLL"   /Gm /EHsc /RTC1 /MDd /Yu"stdafx.h"   /Fp"Debug\server.pch" /Fo"Debug\\" /Fd"Debug\vc80.pdb"   /W3 /nologo /c /Wp64 /ZI /TP /errorReport:prompt  cl.exe /OUT:"U:\libs\Debug\server.dll" /INCREMENTAL:NO /NOLOGO /DLL  /MANIFEST /MANIFESTFILE:"Debug\server.dll.intermediate.manifest"  /DEBUG /PDB:"u:\libs\Debug\server.pdb"  /SUBSYSTEM:WINDOWS /MACHINE:X86 /ERRORREPORT:PROMPT  kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib  shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib 

Client command line:

cl.exe /Od /I "..\server"   /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE"   /Gm /EHsc /RTC1 /MDd /Fo"Debug\\" /Fd"Debug\vc80.pdb" /W3 /c /Wp64 /ZI /TP   .\client.cpp  cl.exe /OUT:"U:\libs\Debug\Debug\client.exe" /INCREMENTAL  /LIBPATH:"U:\libs\Debug"  /MANIFEST /MANIFESTFILE:"Debug\client.exe.intermediate.manifest"  /DEBUG /PDB:"u:\libs\debug\debug\client.pdb"  /SUBSYSTEM:CONSOLE /MACHINE:X86  server.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib  advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib 
like image 669
xtofl Avatar asked Dec 20 '10 12:12

xtofl


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 __ Declspec Dllimport mean?

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

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.

What is __ Declspec in C ++?

The __declspec keyword enables you to specify special attributes of objects and functions. For example, you can use the __declspec keyword to declare imported or exported functions and variables, or to declare Thread Local Storage (TLS) objects.


1 Answers

It isn't required. It is an optimization, a hint to the compiler that the DLL is going to export the function pointer directly rather than just an entry in the IAT of the DLL. The exported function pointer for a function named foo() will be __imp_foo. Which allows it to generate better code, saving a function pointer load from the IAT and an indirect jump. It is a time optimization, not space.

This blog post has the details.

like image 153
Hans Passant Avatar answered Oct 15 '22 00:10

Hans Passant