打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
error C2011: 'fd
我遇到的问题是VS2005中,使用winsock2时,产生错误:
error C2011: 'fd_set' : 'struct' type redefinition
网上找到解决方法如下:
问题描述:在 VC 6.0中使用socket相关的函数时没有什么问题,可是到了.net下就有以下类似的错误,
[C++ Error] winsock2.h(109): E2238 Multiple declaration for 'fd_set'
[C++ Error] winsock.h(54): E2344 Earlier declaration of 'fd_set'
[C++ Error] winsock2.h(112): E2146 Need an identifier to declare
[C++ Warning] winsock2.h(144): W8017 Redefinition of 'FD_SET' is not identical
[C++ Error] winsock2.h(153): E2238 Multiple declaration for 'timeval'
[C++ Error] winsock.h(97): E2344 Earlier declaration of 'timeval'
[C++ Error] winsock2.h(209): E2238 Multiple declaration for 'hostent'
[C++ Error] winsock.h(153): E2344 Earlier declaration of 'hostent'
[C++ Error] winsock2.h(222): E2238 Multiple declaration for 'netent'
[C++ Error] winsock.h(166): E2344 Earlier declaration of 'netent'
[C++ Error] winsock2.h(229): E2238 Multiple declaration for 'servent'
[C++ Error] winsock.h(173): E2344 Earlier declaration of 'servent'
[C++ Error] winsock2.h(241): E2238 Multiple declaration for 'protoent'
[C++ Error] winsock.h(185): E2344 Earlier declaration of 'protoent'
[C++ Error] winsock2.h(327): E2238 Multiple declaration for 'in_addr'
[C++ Error] winsock.h(269): E2344 Earlier declaration of 'in_addr'
[C++ Error] winsock2.h(385): E2238 Multiple declaration for 'sockaddr_in'
[C++ Error] winsock.h(319): E2344 Earlier declaration of 'sockaddr_in'
[C++ Error] winsock2.h(395): E2238 Multiple declaration for 'WSAData'
[C++ Error] winsock.h(329): E2344 Earlier declaration of 'WSAData'
[C++ Error] winsock2.h(411): E2146 Need an identifier to declare
[C++ Warning] winsock2.h(455): W8017 Redefinition of 'SO_DONTLINGER' is not identical
[C++ Warning] winsock2.h(512): W8017 Redefinition of 'AF_IPX' is not identical
[C++ Warning] winsock2.h(540): W8017 Redefinition of 'AF_MAX' is not identical
[C++ Error] winsock2.h(546): E2238 Multiple declaration for 'sockaddr'
[C++ Error] winsock.h(492): E2344 Earlier declaration of 'sockaddr'
[C++ Error] winsock2.h(586): E2238 Multiple declaration for 'sockproto'
[C++ Error] winsock.h(501): E2344 Earlier declaration of 'sockproto'
[C++ Error] winsock2.h(625): E2238 Multiple declaration for 'linger'
[C++ Error] winsock2.h(625): E2228 Too many error or warning messages
Solution:
This problem arises because windows.h (at least, that version of it) includes not winsock2.h butwinsock.h; sadly when Microsoft wrote winsock2.h they chose neither to change windows.h to includewinsock2.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.hstandalone).
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: 'timeval' : '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>
原文地址 http://blog.sina.com.cn/s/blog_4906d564010094p8.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
vc6中 error C2011: 'fd_set' : 'struct' type redefinition 的原因
WinSock.h has already been included
Windows Socket编程简介_网络技术_程序开发_远播教育网
非阻塞模式WinSock编程入门
用WINSOCK实现聊天室的VC++程序设计
Winsock五种IO模型的性能分析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服