引起的bug~


编程经验:一个由<Windows.h>引起的bug~

文章一

  转自:http://www.mamicode.com/info-detail-506772.html

1. 问题描述

最近遇到一个莫名其妙的bug,上网查找,没有找到正确的解决办法,难道大家都没遇到?于是我说一说自己是怎么解决的。

我用的是VS2010,一个解决方案下,添加了多个他人的项目,各个项目配置都使用了库文件(A.lib),编译,唯独其中一个项目,出现了如下错误提示:

 

错误         26     error C2040: “LPCH”:“int”与“int *”的间接寻址级别不同     c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         3539

错误         32     error C2059: 语法错误:“)”        c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h    3564

错误         37     error C2061: 语法错误: 标识符“LPCH”  c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         14149

错误         36     error C2061: 语法错误: 标识符“PLONG”        c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         4990

错误         50     error C2061: 语法错误: 标识符“PLONG”        c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winreg.h   742

错误         51     error C2061: 语法错误: 标识符“PLONG”        c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winreg.h   751

错误         1       error C2143: 语法错误 : 缺少“;”(在“*”的前面)        c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     424

错误         6       error C2143: 语法错误 : 缺少“;”(在“*”的前面)        c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     489

错误         9       error C2143: 语法错误 : 缺少“;”(在“*”的前面)        c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     490

错误         24     error C2143: 语法错误 : 缺少“;”(在“__stdcall”的前面)   c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         3539

错误         28     error C2146: 语法错误: 缺少“)”(在标识符“NewEnvironment”的前面)   c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         3564

错误         52     error C2371: “SHORT”: 重定义;不同的基类型    c:\program files (x86)\microsoftsdks\windows\v7.0a\include\wtypes.h  382

错误         31     error C2440: “初始化”: 无法从“int *”转换为“BOOL”  c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         3564

错误         35     error C2440: “初始化”: 无法从“int *”转换为“BOOL”  c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         3582

错误         34     error C2491: “FreeEnvironmentStringsA”: 不允许 dllimport数据 的定义 c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         3582

错误         30     error C2491: “SetEnvironmentStringsA”: 不允许 dllimport数据 的定义   c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         3564

错误         48     error C2665: “InterlockedCompareExchange”: 2 个重载中没有一个可以转换所有参数类型   c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15393

错误         49     error C2665: “InterlockedCompareExchange”: 3 个重载中没有一个可以转换所有参数类型   c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15404

错误         40     error C2665: “InterlockedDecrement”: 2 个重载中没有一个可以转换所有参数类型        c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15262

错误         41     error C2665: “InterlockedDecrement”: 3 个重载中没有一个可以转换所有参数类型        c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15271

错误         42     error C2665: “InterlockedExchange”: 2 个重载中没有一个可以转换所有参数类型  c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15294

错误         43     error C2665: “InterlockedExchange”: 3 个重载中没有一个可以转换所有参数类型  c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15304

错误         44     error C2665: “InterlockedExchangeAdd”: 2 个重载中没有一个可以转换所有参数类型    c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15328

错误         45     error C2665: “InterlockedExchangeAdd”: 2 个重载中没有一个可以转换所有参数类型    c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15338

错误         46     error C2665: “InterlockedExchangeAdd”: 3 个重载中没有一个可以转换所有参数类型    c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15348

错误         47     error C2665: “InterlockedExchangeAdd”: 3 个重载中没有一个可以转换所有参数类型    c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15358

错误         38     error C2665: “InterlockedIncrement”: 2 个重载中没有一个可以转换所有参数类型          c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15231

错误         39     error C2665: “InterlockedIncrement”: 3 个重载中没有一个可以转换所有参数类型          c:\program files(x86)\microsoft sdks\windows\v7.0a\include\winbase.h       15240

错误         12     error C2733: 不允许重载函数“_bittest”的第二个 C 链接   c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     3656

错误         13     error C2733: 不允许重载函数“_bittestandcomplement”的第二个 C 链接         c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     3662

错误         15     error C2733: 不允许重载函数“_bittestandreset”的第二个 C 链接    c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     3674

错误         14     error C2733: 不允许重载函数“_bittestandset”的第二个 C 链接        c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     3668

错误         18     error C2733: 不允许重载函数“_InterlockedAnd16”的第二个 C 链接 c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     3770

错误         17     error C2733: 不允许重载函数“_interlockedbittestandreset”的第二个 C 链接  c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     3686

错误         16     error C2733: 不允许重载函数“_interlockedbittestandset”的第二个 C 链接      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     3680

错误         19     error C2733: 不允许重载函数“_InterlockedCompareExchange16”的第二个 C 链接 c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h   3777

错误         20     error C2733: 不允许重载函数“_InterlockedOr16”的第二个 C 链接   c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     3783

错误         2       error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     424

错误         3       error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     424

错误         4       error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     424

错误         5       error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     424

错误         7       error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     489

错误         8       error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     489

错误         10     error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     490

错误         11     error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winnt.h     490

错误         25     error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         3539

错误         27     error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int      c:\program files (x86)\microsoftsdks\windows\v7.0a\include\winbase.h         3542

2. 问题原因

其实完全可以忽略最后几条错误,每次找bug,都应该重点关注最开始的几个,后面的bug,很可能是编译器不够完美的原因。经过观察,发现问题出现在winbase.h文件,这就奇怪了,这个头文件是系统文件,不会有任何问题,于是就查找winbase.h的位置,发现其在windows.h中被包含过,而后者是经常被使用的头文件。
windows.h中的相关代码如下所示:

#include <windef.h>

#include <winbase.h>

#include <wingdi.h>

#include <winuser.h>

#if !defined(_MAC) ||defined(_WIN32NLS)

#include <winnls.h>

#endif

#ifndef _MAC

#include <wincon.h>

#include <winver.h>

#endif

#if !defined(_MAC) ||defined(_WIN32REG)

#include <winreg.h>

#endif

#ifndef _MAC

#include <winnetwk.h>

#endif

此时,问题已经显而易见了,肯定是windows.h与winbase.h冲突的原因,更进一步,原因是winbase.h被重复包含了。

3. 问题解决

他人的项目中,肯定引用了winbase.h头文件,不能改他人的项目,那只能改正自己的A.lib了,结果发现A.lib中的头文件,包含了windows.h,赶紧将其放入cpp文件中,重新编译,通过!!!
或者对A.lib中的windows.h,进行预编译限制,也可以解决。

4.总结

1). 看bug就看最开始的几个,前面几个解决了,后面的bug往往自动消失。
2). 不要怀疑系统文件,千锤百炼的东西,不会轻易有问题,问题出在自己身上。
3). 这样莫名其妙的错误,往往是头文件被重复包含的原因,仔细查看头文件即可。查找办法,肯定是先看看出错的文件在那个文件中被包含过,一层一层的去找。

 

文章二

error C2146: 语法错误 : 缺少“;”(在标识符“PVOID64”的前面)

  转自:http://itsdf.blog.51cto.com/2255425/1106989

在用vs2008编译一个dx8项目遇到编译错误:

c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(236) : error C2146: 语法错误 : 缺少“;”(在标识符“PVOID64”的前面)
c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(236) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(7818) : error C2146: 语法错误 : 缺少“;”(在标识符“Buffer”的前面)
c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(7818) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(7818) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int

网上有人说是vs的include顺序问题,也有人说既然是找不到PVOID64加个typedef就行了。经过本人的试验发现,的确是vs的include顺序问题,调整include顺序保证dx目录在sdk目录的下面即可解决问题。下面记录步骤:
1、在vs中,选择 工具->选项->项目和解决方案->VC++ 目录,在 显示以下内容的目录 选择 包含文件,确保条目 $(WindowsSdkDir)\include 和 $(FrameworkSDKDir)include 在任何dx目录的前面。如果没有dx目录也ok(指的不会有这个错误)。如下图:
 

2、在vs的解决方案资源管理器中右键相应的项目,选择属性,在属性窗口的 配置属性 -> C/C++ -> 常规 中的 附加包含目录,点击其右边的小按钮,在弹出的附加包含目录中,确保条目 $(WindowsSdkDir)\include 和 $(FrameworkSDKDir)include 在任何dx目录的前面。如下图:
 

 

 

文章三

转自:http://bbs.csdn.net/topics/390638330

一,问题描述
    本人最开始用的是vs2005,后来由于做手机开发,又安装了vs2008,这时候,项目中的某些文件出现编译错误,后来经过研究,发现原因是:Platform SDK安装到了vs2005的安装目录中,vs2008的项目包含路径中没有包含platfor SDK的include文件。没说的,自己果断加上,解决问题。
    后来,由于公司的要求,又要使用vs2010,所以,电脑上又安装了vs2010,编译项目,出现编译错误:“不允许重载函数“_interlockedbittestandreset”的第二个 C 链 ”,经过网上搜索,发现原因是,由于安装vs2010的时候,自动包含了vs2008中的项目包含目录作为父继承,所以把安装在vs2005中的platform SDK继承下来了。_interlockedbittestandreset函数的定义在platform SDK中的winNT.h和Windows SDK中的winNT.h都有定义。这样,项目中用到的intrin.h文件在调用_interlockedbittestandreset函数的时候,就发生了两个SDK的冲突。
二,解决办法
1,vs2010的项目VC++的包含目录中添加Windows SDK的Include目录。(注意要放到最前的位置,否则可能会出错误:
1>c:\program files\microsoft sdks\windows\v7.0a\include\objidl.h(11280): error C2061: 语法错误: 标识符“__RPC__out_xcount_part”
1>c:\program files\microsoft sdks\windows\v7.0a\include\objidl.h(11281): error C2059: 语法错误:“)”
1>c:\program files\microsoft sdks\windows\v7.0a\include\objidl.h(11281): fatal error C1903: 无法从以前的错误中恢复;正在停止编译)
2,打开目录编辑,去掉旧的platform SDK包含目录
1)打开目录C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\MSBuild\v4.0中的Microsoft.Cpp.Win32.user.props文件
2)<IncludePath>$(IncludePath);$(DXSDK_DIR)Include;$(EMERGENT_PATH)include;$(PHYSXINSTALLPATH)sdks\Physics\include</IncludePath>里去掉旧的platfrom SDK的include目录。
重新编译,搞定!
三,总结
类似的这个错误:
1>c:\program files\microsoft sdks\windows\v7.0a\include\objidl.h(11280): error C2061: 语法错误: 标识符“__RPC__out_xcount_part”
1>c:\program files\microsoft sdks\windows\v7.0a\include\objidl.h(11281): error C2059: 语法错误:“)”
1>c:\program files\microsoft sdks\windows\v7.0a\include\objidl.h(11281): fatal error C1903: 无法从以前的错误中恢复;正在停止编译
原因:
Windows SDK和其它库(如DirectX等)出现冲突。
解决方法如下:
把WindowsSDK的包含目录放在最前!
操作:在项目上单击右键—》属性—》配置属性—》VC++目录,修改“包含目录”,把 $(WindowsSdkDir)include 放在最前。重新编译,搞定!

 

文章四

转自http://www.xuebuyuan.com/1671066.html

cocos2d-x 在IDE vs2010 出现error C2146: 语法错误: 缺少“;”(在标识符“PVOID64”的前面)编译错误解决方案

cocos2d-x 较高版本用到的IDE平台 vs2010 , 安装了vs2010旗舰版.

打开运行cocos2d-x-2.2.0中的HelloCpp项目,居然报错:

error
C2146: c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h  语法错误: 缺少“;”(在标识符“PVOID64”的前面)

error C4430:

c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h  缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 

这两错误是系统配置的问题,网上查找
说是vs的include顺序问题,也有人说既然是找不到PVOID64加个typedef就行了。

对于第二种方法当然是排除(系统文件可不能乱改)。

于是寻求第一种解决方案。

开始的坑人的排雷过程:依别人的做法:

在vs中,选择
工具->选项->项目和解决方案->VC++
目录,在 显示以下内容的目录 选择 包含文件,确保条目 $(WindowsSdkDir)\include 和 $(FrameworkSDKDir)include 在任何dx目录的前面。

我打开一看,我的妈啊
,编辑被否决了。(如果你幸运的话 就直接在这更改nclude顺序)。

 

不让改, 对于IDE不是很熟的我只能google起 visual c++ 2010  VC++目录编辑功能已被否决. 那过程别提了

先是 

在vs的解决方案资源管理器中右键相应的项目,选择属性,在属性窗口的
配置属性 -> C/C++ -> 常规 中的 附加包含目录


发现在这改 只能此项目本身, 无法全局改变。于是又找了全局更改的方法

对于某一项目, 点击菜单:视图-》属性管理器, 弹出属性管理器窗口,点击项目展开如图:

 

 

右击Microsoft.Cpp.Win32.user 弹出属性页 如图

 

 将原本的放在最后的 $(IncludePath) 这宏放到最前面, 将E:\Program Files\DXSDK\Include; 更改放在第二位置上。

确定后重新编译,顺利完成。

 

 

论坛

转自:http://bbs.csdn.net/topics/380219133

1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(296) : error C2146: 语法错误 : 缺少“;”(在标识符“WCHAR”的前面)
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(296) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(299) : error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(299) : error C2040: “PWSTR”: “CONST”与“WCHAR *”的间接寻址级别不同
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(299) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(301) : error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(301) : error C2371: “WCHAR”: 重定义;不同的基类型
1>        c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(289) : 参见“WCHAR”的声明
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(301) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(301) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(302) : error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(302) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(302) : error C2378: “PCWSTR”: 重定义;符号不能由 typedef 重载
1>        c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(301) : 参见“PCWSTR”的声明
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(302) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(303) : error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(303) : error C2371: “WCHAR”: 重定义;不同的基类型
1>        c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(289) : 参见“WCHAR”的声明

谢谢大家说的回答,问题已解决:
将VC项目->配置属性->C/C++->预处理器->预处理器定义->
WIN32
_WINDOWS
_DEBUG
改为
_INTEL
WIN32
_DEBUG
_CONSOLE
_Windows
_CRT_SECURE_NO_DEPRECATE
_WINNT
就可在VC项目中编译C源程序

 

参考文章

文章一:http://bbs.csdn.net/topics/340230893

文章二:http://blog.csdn.net/ljf551551/article/details/16112965

文章三:http://codego.net/436908/

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM