为什么/什么时候__declspec(dllimport)不需要?

时间:2022-04-10 16:29:39

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.

在使用服务器的项目中。dll和客户机。exe,我已经从服务器dll移植了一个服务器符号,而不是dllimport将它移植到客户端exe中。

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

尽管如此,应用程序仍然可以链接并启动,没有任何问题。那么,不需要dllimport吗?

Details:

细节:

I have this 'server' dll:

我有这个“服务器”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

1 个解决方案

#1


46  

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.

它不是必需的。这是一个优化,提示编译器DLL将直接导出函数指针,而不仅仅是DLL的IAT中的一个条目。名为foo()的函数的导出函数指针将是__imp_foo。这使得它可以生成更好的代码,从IAT中保存一个函数指针负载和一个间接跳转。这是一个时间优化,而不是空间。

This blog post has the details.

这篇博文有详细内容。

#1


46  

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.

它不是必需的。这是一个优化,提示编译器DLL将直接导出函数指针,而不仅仅是DLL的IAT中的一个条目。名为foo()的函数的导出函数指针将是__imp_foo。这使得它可以生成更好的代码,从IAT中保存一个函数指针负载和一个间接跳转。这是一个时间优化,而不是空间。

This blog post has the details.

这篇博文有详细内容。