正在做一个远程连接的程序,运用了大量WinAPI函数,但编译链接后,大部分错误都指向Windows.h等Windows自带的头文件,而且大多数都是redefinition,例如:(摘取一部分错误)
c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(168): warning C4005: 'SO_DONTLINGER' : macro redefinition
          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(402) : see previous definition of 'SO_DONTLINGER'
c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(212): error C2011: 'sockaddr' : 'struct' type redefinition
          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(485) : see declaration of 'sockaddr'
c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(390): error C2059: syntax error : 'constant'
c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(390): error C3805: 'constant': unexpected token, expected either '}' or a ','
c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(524): warning C4005: 'IN_CLASSA' : macro redefinition
          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(287) : see previous definition of 'IN_CLASSA'
c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(530): warning C4005: 'IN_CLASSB' : macro redefinition
          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(293) : see previous definition of 'IN_CLASSB'
c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(536): warning C4005: 'IN_CLASSC' : macro redefinition
          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(299) : see previous definition of 'IN_CLASSC'
c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(547): warning C4005: 'INADDR_ANY' : macro redefinition
          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(304) : see previous definition of 'INADDR_ANY'
c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(549): warning C4005: 'INADDR_BROADCAST' : macro redefinition
          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(306) : see previous definition of 'INADDR_BROADCAST'
c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(583): error C2011: 'sockaddr_in' : 'struct' type redefinition
          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(312) : see declaration of 'sockaddr_in'
这是什么原因?
话说我原来头文件中包含了<WinSock2.h>,我改成了<WinSock.h>,就没有这些错误了,这是为什么(我才用的是Sock1.1的版本编程)?WinSock2.h不兼容WinSock.h吗?
我来帮他解答
提问者采纳
检举| 2011-03-25 19:20
把#include <WINSOCK2.H>放在#include <windows.h>前面
追问
请问为什么要这样呢?
回答
因为windows.h里面有些东西要靠一些宏定义来预编译,否则取默认值,所以这些宏定义要在包含windows.h前面定义,而关于winsock的一些定义包含在winsock2。h里面。
如果先windows 后winsock2  那么windows里面已经按默认定义了一些东西,但又遇到winsock2,有些东西就会冲突
提问者评价
明白了,谢啦!