windows.h与winsock2.h的包含顺序

大凡在Windows平台下用C++做网络开发很多时候都会同时包含这两个头文件,如若顺序不当(windows.h先于winsock2.h)就会出现很多莫名其妙的错误。诸如:

警告    4    warning C4005: “AF_IPX”: 宏重定义    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    91
警告    5    warning C4005: “AF_MAX”: 宏重定义    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    127
警告    6    warning C4005: “SO_DONTLINGER”: 宏重定义    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    163
错误    7    error C2011: “sockaddr”: “struct”类型重定义    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    206
错误    8    error C2143: 语法错误 : 缺少“}”(在“常量”的前面)    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    384
错误    9    error C2143: 语法错误 : 缺少“;”(在“常量”的前面)    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    384
错误    10    error C2059: 语法错误 : “常量”    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    384
错误    11    error C2143: 语法错误 : 缺少“;”(在“}”的前面)    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    437
错误    12    error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    437
错误    13    error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    437
警告    14    warning C4005: “IN_CLASSA”: 宏重定义    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    518
警告    15    warning C4005: “IN_CLASSB”: 宏重定义    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    524
警告    16    warning C4005: “IN_CLASSC”: 宏重定义    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h    530
……
错误    133    fatal error C1003: 错误计数超过 100;正在停止编译    f:\yang fan\courses\works\c++\mylamebt\btendpoint\btendpoint.h    267

初看到如此一堆的错误委实不爽,但是只要将二者的包含顺序调换一下问题就会解决,原因参见下面那个链接。另外,上述问题不仅影响直接包含二者的文件,还影响间接包含的情形。比如,a.h包含了windows.h,b.h包含了winsock2.h,如果在c.h当中要引用a.h和b.h,那么正确的顺序应当是b.h先于a.h。当然,实践当中有时很难找到究竟是哪两个文件顺序不对了,终极的解决办法是,在当前工程(就是编译不过的这个工程)所有include语句最前面加上#include <winsock2.h>和#include<windows.h>,世界清静了。

http://blog.chinaunix.net/u2/64540/showart_689402.html

为防止链接失效,敬录如下:

关于WINSOCK.H与winsock2.h中的重定义解决办法分析
问题描述:在 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 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: '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>

转载于:https://www.cnblogs.com/tonyyang132/archive/2009/10/14/1583110.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/262318.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

npoi 导入 winform excel_勤哲Excel服务器做影视制作企业管理系统 - 科技

在人们心中&#xff0c;影视作品的生产是一个系统化的工程&#xff0c;从剧本选择、班底选择&#xff0c;到拍摄和发行&#xff0c;每个环节的信息化和大数据应用&#xff0c;正在释放越来越大的能量。在行业专家们看来&#xff0c;曾经国内影视行业的工业化程度较低&#xff0…

关于 Android 和 iOS 流畅度的一切

2019独角兽企业重金招聘Python工程师标准>>> 之前发表了《论苹果公司的系统拖累策略》&#xff0c;在看了锋友的回复之后&#xff0c;发现无论大家是赞成或者反对&#xff0c;可能都对Android和iOS的认识上存在着一些盲区和误区&#xff0c;于是答应转载这篇专业分析…

java好学还是ui好学_java编程和ui设计哪个好学?

Java语言完全满足了PC的条件,在减少内存,直接在各类电子产品中运行,在网络中可被任何设备所识别,充分利用网络资源等方面有所突破创新,为PC时代的发展创造了有力条件。 随着IT产业的发展,许多企业中的计算机应用程序也在随之转型,企业中所应用的计算机模式逐渐向客户端较小、服…

Extjs4 MVC 添加view层

如果这不是您感兴趣或者需要学习的东西&#xff0c;何必点开呢&#xff1f;人生苦短。实例中我们通过view层望主界面中添加一个grid&#xff0c; 1、app.js改成如下&#xff1a; app.js 1 Ext.Loader.setConfig({enabled:true});//开启动态加载2 Ext.application({3 name: …

关于企业管理信息系统

其实一个软件产品本身并无多大意义&#xff0c;关键在于用的思路和过程。 进入目前这家公司后&#xff0c;接到的第一个任务就是公司CRM选型与实施。选型上一任已经完成&#xff0c;是奥汀CRM IV版&#xff0c;只待付款购买了。 在我最初的印象中&#xff0c;管理软件都是很简单…

剪映电脑版_2020 年双十一要不要选一个平板电脑?

其实大路很久以前就一直在关注平板电脑&#xff0c;我记得前几年平板电脑有好多厂家都在做&#xff0c;比如小米的红米平板&#xff0c;1000元左右&#xff0c;性能像手机一样&#xff0c;非常香。华为也有很多&#xff0c;但是今年我突然想买的时候&#xff0c;尼玛停产的停产…

Qt之自定义搜索框

简述 关于搜索框&#xff0c;大家都经常接触。例如&#xff1a;浏览器搜索、Windows资源管理器搜索等。 当然&#xff0c;这些对于Qt实现来说毫无压力&#xff0c;只要思路清晰&#xff0c;分分钟搞定。 方案一&#xff1a;调用QLineEdit现有接口 void addAction(QAction * act…

java string类型_java中String类型

String类型是字符串类型。。字符串一旦创建不可以在改变。“abc”字符串对象一旦创建&#xff0c;不可以再改成“abcd”提升字符串的访问效率&#xff1a;在程序中使用了“缓存”技术。所以在java中所有使用“双引号”括起来的字符串都会在“字符串常量池”中创建一份。字符串常…

pandaboard ES学习之旅——2 ES环境搭建

2 ES环境搭建 2.1 环境准备 Panda ES开发板1块&#xff1b; 5v电源1根&#xff0c;4G SD卡1个&#xff1b; 串口线1根&#xff08;电脑波特率设置为115200&#xff09;&#xff1b; HDMI转DVI视频线1根&#xff08;HDMI接头连开发板P1DVI-D&#xff0c;DVI接头连显示器&#xf…

跟我学Windows7的33个技巧(二)

18. 自定义电源开关 默认情况下&#xff0c;Windows 7在开始菜单处将以文本形式显示关机按钮&#xff0c;但是你只需要几秒钟就可以将这种默认的方式改变。如果每天你都需要重启电脑许多次&#xff0c;那么这样的改变比默认的方式更加有趣&#xff0c;右击开始菜单选择属性“Pr…

android gps 锁屏更新坐标_把手机锁屏设置成任意字体,悄悄给男(女)朋友一个惊喜吧...

有好软件就要与大家分享&#xff0c;我是阿喵&#xff0c;阿喵每天会定时推送出安卓手机、苹果手机、电脑相关、学习等优质软件&#xff0c;如果各位觉得软件不错&#xff0c;就请转发评论&#xff0c;留言需要什么样功能的&#xff0c;留言需要什么样功能的&#xff0c;阿喵定…

java 队列 array_Java源码解析阻塞队列ArrayBlockingQueue常用方法

本文基于jdk1.8进行分析首先看一下ArrayBlockingQueue的成员变量。如下图。最主要的成员变量是items&#xff0c;它是一个Object类型的数组用于保存阻塞队列中的元素。其次是takeIndex&#xff0c;putIndex&#xff0c;count&#xff0c;分别表示了从队列获取元素的位置&#x…

nginx的502错误及常见解决方法汇总

nginx以其高性能以及对物理计算资源的高密度利用&#xff0c;展示了较apache更为优越的性能&#xff0c;目前很多企业都更加倾向于使用nginx架构的web服务&#xff0c;但是我们在安装配置与配置的过程中经常会遇到502的错误&#xff0c;那么引起这些错误的原因有哪些&#xff0…

实验一:SQL server 2005高可用性之----日志传送

如转载&#xff0c;请注明出处&#xff1a;http://blog.csdn.net/robinson_0612/archive/2009/10/31/4751070.aspx SQL server 2005高可用性之日志传送是在SQL server 2000 日志传送基础之上的延续&#xff0c;两者其本质上并没有太大的差异。日志传送能够同步位于不同服务器或…

反向代理服务器

一 反向代理服务器简介      反向代理&#xff08;Reverse Proxy&#xff09;方式是指以代理服务器来接受Internet上的连接请求&#xff0c;然后将请求转发给内部网络上的服务器&#xff1b;并将从服务器上得到的结果返回给Internet上请求连接的客户端&#xff0c;此时代…

网页制作代码模板_科普:关于网页设计的4个基础小知识

网站是企业展示形象、引流营销的重要互联网窗口&#xff0c;因此一定要设计得足够吸睛美观&#xff0c;引起顾客兴趣。今天就来教大家如何做好网页设计与制作。提到网页设计&#xff0c;可能很多小白都要问了&#xff1a;网页设计用什么软件做&#xff1f;网页设计需要学什么&a…

Spock Primer 翻译

起因 最近要搞groovy介绍&#xff0c;准备做成一系列的东西&#xff0c;参考github上的计划。https://github.com/javahub/groovy_hello spock没有找到翻译文档&#xff0c;动手把最重要的一章primer翻译下&#xff0c;想起了c primer。就当作翻译练习了。 目前项目大使用spock…

java如何设置classpath_Java如何设置ClassPath路径?

因为有很多汉字需要编码所以选择gbkURIEncoding"GBK"/etc/profileexport PATHexport JAVA_HOME/usr/lib/jvm/java-6-sunexport CLASSPATH。:$JAVA_HOME/lib/dt。jar:$JAVA_HOME/lib/tools。jar:$JAVA_HOME/lib/mysql-connector-java-5。1。12-bin。jarumask 022expor…

随机数尽可能做到不重复

随机数尽可能做到不重复 使用Guid.NewGuid().GetHashCode()作为种子&#xff0c;可以确保Random在极短时间产生的随机数尽可能做到不重复 Random rand new Random(Guid.NewGuid().GetHashCode()); int r rand.Next(1000,10000); posted on 2009-11-03 11:43 搏击的小船…

Tomcat6.0 中数据源的配置

前段时间换了Tomcat6.0。一直都没配置mysql数据库数据源。今天想用时&#xff0c;既然按以前版本的方式配不上。。汗。。。后来上网查找才找到解决方案。写下这段话&#xff0c;以免忘记。。具体做法如下&#xff1a; 在$CATALINA_HOME/conf/server.xml文件中的<Host><…