WinSock2.h 中的重新定义错误

2021-12-20 00:00:00 visual-studio windows networking winapi c++

在项目文件中包含 winsock2.h.

Including winsock2.h, in a file of a project.

在 -
中遇到很多错误WinSock2.h(109) : error C2011: 'fd_set' : 'struct' 类型重新定义
带有相应的 -
WinSock.h(54) : 见'fd_set'的声明

我没有看到任何标题中包含 winsock.h.

I don't see winsock.h included in any of the headers.

项目中是否有任何设置可能导致这种情况?

Are there any setting in the project that may be causing this?

编辑
但是,我在另一个文件中使用 windows.h:
http://cboard.cprogramming.com/windows-programming/41212-奇怪的msvc-winsock2-compile-error.html

编辑 2
在标题中我有:

EDIT 2
In the header I have:

#include <winsock2.h>  
#include <iphlpapi.h>
#include "MyFilesFileX.h" <-which #include <windows.h> 

推荐答案

我有根据的猜测是包含标题的顺序,即首先包含 winsock2.h(第一个含义在 windows 之前.h),或者在包含 windows.h 之前定义 WIN32_LEAN_AND_MEAN,这样可以防止 windows.h 包含 winsock v1.

My educated guess would be the order of included headers, i.e. include winsock2.h first (with first meaning before windows.h), or define WIN32_LEAN_AND_MEAN before including windows.h, which prevents windows.h from including winsock v1.

#include <winsock2.h>
#include <windows.h>

-或-

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>

相关文章