注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

_

_

 
 
 

日志

 
 

关于WINSOCK.H与winsock2.h中的重定义解决办法分析  

2014-04-26 02:42:43|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

关于WINSOCK.H与winsock2.h中的重定义解决办法分析

(2009-12-15 10:21:57)

关于WINSOCK.H与winsock2.h中的重定义解决办法分析 - redtea - _转载

标签:

vc开发

分类: Windows

问题描述:在 VC 6.0中使用socket相关的函数时没有什么问题,可是到了.net下就有以下类似的错误,

c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(112) : error C2011: “fd_set”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(54) : 参见“fd_set”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(147) : warning C4005: “FD_SET”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(88) : 参见“FD_SET”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(156) : error C2011: “tim”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(97) : 参见“tim”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(212) : error C2011: “hostent”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(153) : 参见“hostent”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(225) : error C2011: “netent”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(166) : 参见“netent”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(232) : error C2011: “servent”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(173) : 参见“servent”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(244) : error C2011: “protoent”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(185) : 参见“protoent”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(400) : error C2011: “sockaddr_in”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(321) : 参见“sockaddr_in”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(410) : error C2011: “WSAData”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(331) : 参见“WSAData”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(470) : warning C4005: “SO_DONTLINGER”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(411) : 参见“SO_DONTLINGER”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(527) : warning C4005: “AF_IPX”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(469) : 参见“AF_IPX”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(555) : warning C4005: “AF_MAX”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(488) : 参见“AF_MAX”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(561) : error C2011: “sockaddr”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(494) : 参见“sockaddr”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(601) : error C2011: “sockproto”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(503) : 参见“sockproto”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(640) : error C2011: “linger”: “struct”类型重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(540) : 参见“linger”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(653) : warning C4005: “SOMAXCONN”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(553) : 参见“SOMAXCONN”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(679) : warning C4005: “FD_READ”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(571) : 参见“FD_READ”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(682) : warning C4005: “FD_WRITE”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(572) : 参见“FD_WRITE”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(685) : warning C4005: “FD_OOB”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(573) : 参见“FD_OOB”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(688) : warning C4005: “FD_ACCEPT”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(574) : 参见“FD_ACCEPT”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(691) : warning C4005: “FD_CONNECT”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(575) : 参见“FD_CONNECT”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(694) : warning C4005: “FD_CLOSE”: 宏重定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(576) : 参见“FD_CLOSE”的前一个定义
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(1626) : error C2375: “accept”: 重定义;不同的链接
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(744) : 参见“accept”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(1647) : error C2375: “bind”: 重定义;不同的链接
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(749) : 参见“bind”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(1666) : error C2375: “closesocket”: 重定义;不同的链接
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(754) : 参见“closesocket”的声明
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(1685) : error C2375: “connect”: 重定义;不同的链接
c:\program files\microsoft visual studio 8\vc\platformsdk\include\winsock.h(756) : 参见“connect”的声明

Solution:

This problem arises because windows.h (at least, that version of it) includes not winsock2.h but winsock.h; sadly when Microsoft wrote winsock2.h they chose neither to change windows.h to include winsock2.h, which replaces winsock.h, nor to include windows.h from winsock2.h and then add the definitions for the new Winsock 2 API methods & structures (this might seem reasonable since Winsock 2 does, strictly speaking, replace Winsock 1, but since the API must be fully backwards-compatible the distinction is somewhat meaningless and there's no real benefit to making winsock2.h standalone).

The fix is thankfully simple: always "#include <winsock2.h>" before windows.h.

However, you must remember that if windows.h has been included by (for example) a higher-level header file that is subsequently including your header file, it's too late - so you must make sure that the higher-level header files respect this convention also.

It is however rarely necessary to modify the header files of libraries or other code modules you are using just because you include their header files, and their header files include windows.h - you can just include winsock2.h before you include the library's header files.

在包含jrtplib有时候我也遇到这个问题,解决方法与之相同。一句话,在#include<windows.h>之前 #include <winsock2.h> 问题就可以解决。

问题描述]
在包含了<windows.h>以及<winsock2.h>的工程中,编译有时会出现如
下错误:
error C2011: 'fd_set' : 'struct' type redefinition
error C2011: 'tim' : 'struct' type redefinition
....
error C2375: 'accept' : redefinition; different linkage
[原因分析]
主要原因是因为<windows.h>中包含了<winsock.h>头文件,由于其版
本的不同,导致出现上述的错误。<windows.h>中相关代码如下:
#ifndef WIN32_LEAN_AND_MEAN
#include <cderr.h>
#include <dde.h>
#include <ddeml.h>
........
#ifndef _MAC
#include <winperf.h>
#include <winsock.h>
#endif
.......
#include <commdlg.h>
#endif
#endif
[解决方案]
由以上代码可以看出如果在没有定义WIN32_LEAN_AND_MEAN宏的大前
提下windows.h有可能包含winsock.h 头文件,因此我们得出一个很简单
的解决方法就是在包含<windows.h>之前定义WIN32_LEAN_AND_MEAN宏,如
下所示:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

  评论这张
 
阅读(983)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017