为什么MinGW-w64需要手动包含winsock2? [英] Why does MinGW-w64 require winsock2 to be manually included?

查看:1166
本文介绍了为什么MinGW-w64需要手动包含winsock2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些代码和相关的警告/错误:

Some code and associated warnings/errors:

#include <windows.h>
#include <iphlpapi.h>
int main() { }




F:/ Prog /mingw-w64/x86_64-4.9.2-win32-seh-rt_v3-rev1/mingw64/x86_64-w64-mingw32/include/ws2ipdef.h:71:3:错误:' ADDRESS_FAMILY '未命名类型

ADDRESS_FAMILY si_family;




#include <windows.h>
#include <winsock2.h>
#include <iphlpapi.h>
int main() { }     // this compiles successfully, only gives warning




F:/Prog/mingw-w64/x86_64-4.9.2-win32-seh-rt_v3-rev1/mingw64/x86_64-w64-mingw32/include/winsock2.h:15:2:warning:#warning请在windows.h [-Wcpp]之前加入winsock2.h。

F:/Prog/mingw-w64/x86_64-4.9.2-win32-seh-rt_v3-rev1/mingw64/x86_64-w64-mingw32/include/winsock2.h:15:2: warning: #warning Please include winsock2.h before windows.h [-Wcpp]



#include <winsock2.h>
#include <windows.h>
#include <iphlpapi.h>
int main() { }




(compilation successful).

我的问题是:


  • 为什么 iphlpapi.h (和其他依赖于winsock的文件)do #include< winsock2.h>

  • Why doesn't iphlpapi.h (and other files that rely on winsock) do #include <winsock2.h> themself, or at least give a nicer error message?
  • Why does there a warning for it to come before windows.h ?

推荐答案

windows.h winsock.h winsock.h winsock2.h 不共享。如果首先包括 winsock2.h ,它会禁用 winsock.h ,一切都很好。但如果首先包括 winsock.h ,则 winsock2.h 无法编译,因为它重新声明了很多事情 winsock.h 已声明。因此,为什么你需要首先包括 winsock2.h

windows.h includes winsock.h by default. winsock.h and winsock2.h do not coexist. If winsock2.h is included first, it disables winsock.h, and all is good. But if winsock.h is included first, winsock2.h fails to compile, because it redeclares a lot of things that winsock.h already declares. Thus why you need to include winsock2.h first.

Microsoft希望人们使用其中一个,都。 winsock2.h 意在替换 winsock.h ,而不是 / em>它。但是为什么他们让 winsock2.h 如此不兼容,如果 winsock.h 包含在第一个是完全打破任何人的猜测。他们可以很容易地使 winsock2.h #ifdef 任何重复的声明,以避免错误。或者甚至为声明包含 winsock2.h include winsock.h 。但他们没有。

Microsoft wants people to use one or the other, not both. winsock2.h is meant to replace winsock.h rather than supplement it. But why they made winsock2.h so incompatible that it completely breaks if winsock.h is included first is anyone's guess. They could have just as easily made winsock2.h #ifdef any duplicate declarations to avoid errors. Or even have winsock2.h include winsock.h for the declarations. But they didn't.

这篇关于为什么MinGW-w64需要手动包含winsock2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆