디버깅

error c2011 'sockaddr' 'struct' type redefinition

Mesia 2015. 7. 18. 11:38

2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(100): warning C4005: 'AF_IPX' : macro redefinition
2>          c:\program files (x86)\windows kits\8.1\include\um\winsock.h(452) : see previous definition of 'AF_IPX'
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(140): warning C4005: 'AF_MAX' : macro redefinition
2>          c:\program files (x86)\windows kits\8.1\include\um\winsock.h(471) : see previous definition of 'AF_MAX'
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(177): warning C4005: 'SO_DONTLINGER' : macro redefinition
2>          c:\program files (x86)\windows kits\8.1\include\um\winsock.h(394) : see previous definition of 'SO_DONTLINGER'
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(221): error C2011: 'sockaddr' : 'struct' type redefinition
2>          c:\program files (x86)\windows kits\8.1\include\um\winsock.h(477) : see declaration of 'sockaddr'
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(421): error C2059: syntax error : 'constant'
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(421): error C3805: 'constant': unexpected token, expected either '}' or a ','
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(556): warning C4005: 'IN_CLASSA' : macro redefinition
2>          c:\program files (x86)\windows kits\8.1\include\um\winsock.h(279) : see previous definition of 'IN_CLASSA'
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(562): warning C4005: 'IN_CLASSB' : macro redefinition
2>          c:\program files (x86)\windows kits\8.1\include\um\winsock.h(285) : see previous definition of 'IN_CLASSB'
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(568): warning C4005: 'IN_CLASSC' : macro redefinition
2>          c:\program files (x86)\windows kits\8.1\include\um\winsock.h(291) : see previous definition of 'IN_CLASSC'
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(579): warning C4005: 'INADDR_ANY' : macro redefinition
2>          c:\program files (x86)\windows kits\8.1\include\um\winsock.h(296) : see previous definition of 'INADDR_ANY'
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(581): warning C4005: 'INADDR_BROADCAST' : macro redefinition
2>          c:\program files (x86)\windows kits\8.1\include\um\winsock.h(298) : see previous definition of 'INADDR_BROADCAST'
2>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(615): error C2011: 'sockaddr_in' : 'struct' type redefinition
2>          c:\program files (x86)\windows kits\8.1\include\um\winsock.h(304) : see declaration of 'sockaddr_in'
2>c:\program files (x86)\windows kits\8.1\include\um\winsock2.h(136): error C2011: 'fd_set' : 'struct' type redefinition

 

 

As others suggested, the problem is when windows.h is included before WinSock2.h. Because windows.h includes winsock.h. You can not use both WinSock2.h and winsock.h.

Solutions:

  • Include WinSock2.h before windows.h. In the case of precompiled headers, you should solve it there. In the case of simple project, it is easy. However in big projects (especially when writing portable code, without precompiled headers) it can be very hard, because when your header with WinSock2.h is included, windows.h can be already included from some other header/implementation file.

  • Define WIN32_LEAN_AND_MEAN before windows.h or project wide. But it will exclude many other stuff you may need and you should include it by your own.

  • Define _WINSOCKAPI_ before windows.h or project wide. But when you include WinSock2.h you get macro redefinition warning.

  • Use windows.h instead of WinSock2.h when winsock.h is enough for your project (in most cases it is). This will probably result in longer compilation time but solves any errors/warnings.