我发现定义WIN32_LEAN_AND_MEAN的解释是“通过排除一些不太常用的api来减小Win32头文件的大小”。我在其他地方读到过,它加快了构建过程。
那么WIN32_LEAN_AND_MEAN到底排除了什么呢?我应该关心这个预处理器指令吗?它是否加快了构建过程?
我还在项目中看到过一个预处理器指令,命名为“额外精益”。这是我该知道的另一个深奥的预处理咒语吗?
我发现定义WIN32_LEAN_AND_MEAN的解释是“通过排除一些不太常用的api来减小Win32头文件的大小”。我在其他地方读到过,它加快了构建过程。
那么WIN32_LEAN_AND_MEAN到底排除了什么呢?我应该关心这个预处理器指令吗?它是否加快了构建过程?
我还在项目中看到过一个预处理器指令,命名为“额外精益”。这是我该知道的另一个深奥的预处理咒语吗?
直接从Windows.h头文件:
#ifndef WIN32_LEAN_AND_MEAN
#include <cderr.h>
#include <dde.h>
#include <ddeml.h>
#include <dlgs.h>
#ifndef _MAC
#include <lzexpand.h>
#include <mmsystem.h>
#include <nb30.h>
#include <rpc.h>
#endif
#include <shellapi.h>
#ifndef _MAC
#include <winperf.h>
#include <winsock.h>
#endif
#ifndef NOCRYPT
#include <wincrypt.h>
#include <winefs.h>
#include <winscard.h>
#endif
#ifndef NOGDI
#ifndef _MAC
#include <winspool.h>
#ifdef INC_OLE1
#include <ole.h>
#else
#include <ole2.h>
#endif /* !INC_OLE1 */
#endif /* !MAC */
#include <commdlg.h>
#endif /* !NOGDI */
#endif /* WIN32_LEAN_AND_MEAN */
如果您想知道每个头文件的实际功能,在MSDN库中输入头文件名称,通常会生成该头文件中的函数列表。
此外,从微软的支持页面:
为了加快构建过程,Visual c++和Windows头文件提供了 以下是新的定义: VC_EXTRALEAN WIN32_LEAN_AND_MEAN 您可以使用它们来减小Win32头文件的大小。
最后,如果您选择使用这些预处理器定义中的任何一个,而缺少所需的某些内容,您可以自己包含特定的头文件。在MSDN中输入你想要的函数名,通常会在页面底部产生一个条目,它会告诉你如果你想使用它,应该包括哪个标题。
补充上面的答案,也“鹦鹉学舌”从Windows Dev Center文档,
The Winsock2.h header file internally includes core elements from the Windows.h header file, so there is not usually an #include line for the Windows.h header file in Winsock applications. If an #include line is needed for the Windows.h header file, this should be preceded with the #define WIN32_LEAN_AND_MEAN macro. For historical reasons, the Windows.h header defaults to including the Winsock.h header file for Windows Sockets 1.1. The declarations in the Winsock.h header file will conflict with the declarations in the Winsock2.h header file required by Windows Sockets 2.0. The WIN32_LEAN_AND_MEAN macro prevents the Winsock.h from being included by the Windows.h header ..
以下是Raymond Chen博客中关于动机的一个很好的回答: https://devblogs.microsoft.com/oldnewthing/20091130-00/?p=15863
...defining WIN32_LEAN_AND_MEAN brought you back to the 16-bit Windows philosophy of a minimal set of header files for writing a bare-bones Windows program. This appeased the programmers who liked to micro-manage their header files, and it was a big help because, at the time the symbol was introduced, precompiled header files were not in common use. As I recall, on a 50MHz 80486 with 8MB of memory, switching to WIN32_LEAN_AND_MEAN shaved three seconds off the compile time of each C file. When your project consists of 20 C files, that’s a whole minute saved right there.