关于"windows.h先于winsock2.h被包含"除了定义WIN32_LEAN_AND_MEAN宏,有没别的解决方法?

weiwuyuan 2012-03-12 10:02:32
如题...
...全文
185 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
王林森 2013-11-30
  • 打赏
  • 举报
回复
这个宏定义在http://msdn.microsoft.com/en-us/library/windows/desktop/ms738562%28v=vs.85%29.aspx有一段描述,部分援引如下。 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_MEANmacro. 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. The WIN32_LEAN_AND_MEANmacro prevents the Winsock.h from being included by the Windows.h header. 意思就是: 头文件Winsock2.h在内部包含了Windows.h的核心元素,因此在Winsock应用程序中通常不包含头文件Windows.h。如果需要一个#include行来包含Windows.h,那么就需要在前面包含#define WIN32_LEAN_AND_MEAN 宏。由于历史原因,头文件Windows.h默认地包含Windows Sockets 1.1版的头文件Winsock.h。在头文件Winsock.h中的声明会和Windows Socket2需要的头文件Winsock2.h中的声明冲突。WIN32_LEAN_AND_MEAN宏防止Windows.h包含Winsock.h。
StarsunYzL 2012-03-12
  • 打赏
  • 举报
回复
在网络模块的头文件检测是否包含了windows.h,如果已经包含了,则提示错误、停止编译,让使用这个模块的人自己去处理。

#ifdef _WINDOWS_
#error "本网络模块头文件必须优先在windows.h头文件之前被包含"
#endif

//#define WIN32_LEAN_AND_MEAN
elude 2012-03-12
  • 打赏
  • 举报
回复
貌似也没什么好办法,还是判断宏比较方便吧
weiwuyuan 2012-03-12
  • 打赏
  • 举报
回复
之所以把"windows.h先于winsock2.h被包含"当做前提,是因为:
对于一个网络模块来讲,他不知道使用这个模块的人之前是否已经包含windows.h了,所以,必须考虑如果先包含了,该怎么解决。
weiwuyuan 2012-03-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 visualeleven 的回复:]
先#include <winsock2.h>再#include <Windows.h>
因为Windows.h里包含了winsock.h头文件
winsock2.h和winsock.h有很多是相互冲突的
[/Quote]

我说的是:windows.h先于winsock2.h被包含
Eleven 2012-03-12
  • 打赏
  • 举报
回复
先#include <winsock2.h>再#include <Windows.h>
因为Windows.h里包含了winsock.h头文件
winsock2.h和winsock.h有很多是相互冲突的

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧