最近做IPV6兼容升级,发现包含了winsock2.h后,会出现编译报错。部分报错信息如下:


winsock2.h(109) : error C2011: 'fd_set' : 'struct' type redefinition
winsock2.h(144) : warning C4005: 'FD_SET' : macro redefinition
winsock.h(88)   : see previous definition of 'FD_SET'
winsock2.h(153) : error C2011: 'timeval' : 'struct' type redefinition
winsock2.h(209) : error C2011: 'hostent' : 'struct' type redefinition
winsock2.h(222) : error C2011: 'netent' : 'struct' type redefinition
winsock2.h(229) : error C2011: 'servent' : 'struct' type redefinition
winsock2.h(241) : error C2011: 'protoent' : 'struct' type redefinition
winsock2.h(397) : error C2011: 'sockaddr_in' : 'struct' type redefinition
winsock2.h(407) : error C2011: 'WSAData' : 'struct' type redefinition


经过调查,发现是由于我原来工程引入的windows.h和winsock2.h冲突,引发了重定义错误。


附上解决方案:

一、官方备注解决方案:

使用宏   #define WIN32_LEAN_AND_MEAN

参考文档:WIN32_LEAN_AND_MEAN宏的官网示例

Winsock2.h与windows.h重定义问题解决

 

二、头文件先后顺序调整

在包含windows.h前,先包含winsock2.h

示例如下:

#include "winsock2.h"

#include <windows.h>

相关文章:

  • 2021-12-13
  • 2021-10-02
  • 2021-11-14
  • 2021-08-19
  • 2021-09-10
  • 2021-05-29
  • 2022-01-02
  • 2021-11-29
猜你喜欢
  • 2021-12-16
  • 2022-01-06
  • 2022-01-08
  • 2021-12-30
  • 2021-04-08
  • 2021-09-01
  • 2021-08-03
相关资源
相似解决方案