MSVC-Wall中标准头文件中的数千个警告是什么?

时间:2010-10-22 23:55:41

标签: c++ visual-c++ compiler-warnings

有些人似乎建议你使用-Wall,但是当我在一个只有一些包含main.cpp的小型测试项目上做的时候,我会在标准头文件或windows头文件中得到5800个警告。 / p>

这是预期的行为吗?如何免费编写编译警告?

以下是一些阅读乐趣:

1>c:\program files\microsoft visual studio 10.0\vc\include\stdint.h(105): warning C4668: '_INTPTR' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(109): warning C4820: '_wfinddata64i32_t' : '4' bytes padding added after data member '_wfinddata64i32_t::attrib'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(114): warning C4820: '_wfinddata64i32_t' : '4' bytes padding added after data member '_wfinddata64i32_t::name'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(118): warning C4820: '_wfinddata64_t' : '4' bytes padding added after data member '_wfinddata64_t::attrib'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(488): warning C4820: '_stat32' : '2' bytes padding added after data member '_stat32::st_gid'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(504): warning C4820: 'stat' : '2' bytes padding added after data member 'stat::st_gid'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(520): warning C4820: '_stat32i64' : '2' bytes padding added after data member '_stat32i64::st_gid'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(521): warning C4820: '_stat32i64' : '4' bytes padding added after data member '_stat32i64::st_rdev'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(525): warning C4820: '_stat32i64' : '4' bytes padding added after data member '_stat32i64::st_ctime'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(534): warning C4820: '_stat64i32' : '2' bytes padding added after data member '_stat64i32::st_gid'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(548): warning C4820: '_stat64' : '2' bytes padding added after data member '_stat64::st_gid'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(549): warning C4820: '_stat64' : '4' bytes padding added after data member '_stat64::st_rdev'
1>c:\program files\microsoft visual studio 10.0\vc\include\crtdbg.h(1078): warning C4986: 'operator new[]': exception specification does not match previous declaration
1>          c:\program files\microsoft visual studio 10.0\vc\include\new(79) : see declaration of 'operator new[]'
1>c:\program files\microsoft visual studio 10.0\vc\include\crtdbg.h(1095): warning C4986: 'operator delete[]': exception specification does not match previous declaration
1>          c:\program files\microsoft visual studio 10.0\vc\include\new(77) : see declaration of 'operator delete[]'
1>c:\program files\microsoft visual studio 10.0\vc\include\typeinfo(76): warning C4820: 'type_info' : '3' bytes padding added after data member 'type_info::_M_d_name'
1>c:\program files\microsoft sdks\windows\v7.0a\include\basetsd.h(114): warning C4668: '__midl' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8154): warning C4820: '_SECURITY_QUALITY_OF_SERVICE' : '2' bytes padding added after data member '_SECURITY_QUALITY_OF_SERVICE::EffectiveOnly'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8165): warning C4820: '_SE_IMPERSONATION_STATE' : '2' bytes padding added after data member '_SE_IMPERSONATION_STATE::EffectiveOnly'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8334): warning C4820: '_QUOTA_LIMITS' : '4' bytes padding added after data member '_QUOTA_LIMITS::PagefileLimit'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8357): warning C4820: '_QUOTA_LIMITS_EX' : '4' bytes padding added after data member '_QUOTA_LIMITS_EX::PagefileLimit'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8405): warning C4820: '_JOBOBJECT_BASIC_LIMIT_INFORMATION' : '4' bytes padding added after data member '_JOBOBJECT_BASIC_LIMIT_INFORMATION::SchedulingClass'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8984): warning C4820: '_FILE_NOTIFY_INFORMATION' : '2' bytes padding added after data member '_FILE_NOTIFY_INFORMATION::FileName'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(9012): warning C4820: '_REPARSE_GUID_DATA_BUFFER' : '3' bytes padding added after data member '_REPARSE_GUID_DATA_BUFFER::GenericReparseBuffer'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(10131): warning C4820: '<unnamed-tag>' : '3' bytes padding added after data member '<unnamed-tag>::Data'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(10241): warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member '<unnamed-tag>::DecreaseTime'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(10262): warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member '<unnamed-tag>::TimerInterval' 
1>c:\program files\microsoft sdks\windows\v7.0a\include\wincrypt.h(1440): warning C4668: 'NTDDI_WINLH' is not defined as a preprocessor macro, replacing with '0' for '#if/

6 个答案:

答案 0 :(得分:38)

Visual C ++ /Wall启用默认情况下/W4禁用的所有警告。正如您所知,有一个很好的理由为什么默认情况下禁用了很多警告(感谢编译器,告诉我你已经添加了填充;我真的很感激它!)。最好只在Visual C ++上使用/W4

英特尔C ++也是这样(我不知道其他使用EDG前端的编译器)。如果将其设置为/W5,则会发出大量信息性消息。我个人最喜欢的是,如果存储类说明符不在声明的开头,它会发出警告(因此,const static int不行,但static const int没问题。)

答案 1 :(得分:27)

要禁用无法控制的系统标头中的警告,只需使用此结构:

#pragma warning(push, 0)       
//Some includes with unfixable warnings
#pragma warning(pop)

或更有选择性地针对特定警告:

#pragma warning( push )
#pragma warning( disable : 4081)
#pragma warning( disable : 4706 )
// system header includes 
#pragma warning( pop )

这个答案来自另一个Stack Overflow线程:(https://stackoverflow.com/questions/2541984/how-to-suppress-warnings-in-external-headers-in-visual-c)。

我完全赞同“edA-qa mort-ora-y”的评论。我希望在我的代码中看到所有警告,包括重要的东西,如C4265(DTOR非虚拟)。虽然C4265处于警告级别3,但微软在他们的智慧中默认将其关闭,你需要/ Wall才能获得它。有关隐藏哪些警告的详细信息,请参阅此页面:

http://msdn.microsoft.com/en-GB/library/23k5d385(v=vs.80).aspx

要查看这些并抑制来自外部标题的噪音,此页面提供了很好的建议,我认为完全回答了启动此主题的原始问题:

http://blogs.msdn.com/b/vcblog/archive/2010/12/14/off-by-default-compiler-warnings-in-visual-c.aspx

基本上它建议用适当的#pragmas创建一个'全局'包含文件来抑制你不关心的警告(可能是C4820的填充),以上述方式防止外部头文件,然后用/ Wall编译。这是一项工作,但值得。在GCC下,这只是一个使用-isystem的问题。微软开发:注意! VS是一个聪明的产品,但有时用简单的东西真的很蠢。

答案 2 :(得分:8)

我知道这是游戏的后期,但我相信我有办法使用/ Wall作为你自己的文件,但必须看到来自微软或其他“外部”标题的噪音。这假设您通过stdafx.h使用预编译头文件。

  1. 对于项目,将警告级别设置为/ Wall(最大警告)
  2. 对于文件stdafx.cpp,将警告级别设置为/ W4(启用了大量警告,但MS标头静默传递)
  3. 对于项目,在“禁用特定警告”下,添加4652
  4. 前两个看起来很明显。但是当stdafx.h包含在您自己的文件中时,警告级别不匹配并发出警告C4652。这打败了整个演习。但现在该消息也被抑制了。

    为每个新项目执行此操作有点乏味,但不如许多单独的#pragma warning()抑制那样糟糕。

答案 3 :(得分:6)

对于MSVC使用/W4

答案 4 :(得分:2)

在MSVC 2010中

选项

配置属性

<强> C / C ++

高级

停用特定警告

设置值 4820; 4996; 4514; 4710 明确禁用您认为不重要的警告。 此时,您可以 -WALL 无忧无虑

答案 5 :(得分:1)

在使用VC6和Microsoft Platform SDK编译各种软件堆栈时(例如在BaseTsd.h中),我遇到了同样的初始问题。

我(我们)想要做的是控制代码的编译器警告级别 - 我们希望能够使用/ W标志。实际上,正如已经指出的那样,/ W4可以启用所有通常有用的警告(还有一些虚假的......)。

由于问题来自MSFT头文件,我以干净的方式修改了Microsoft提供的头文件。实施的改变并不多。

如果编译器抱怨C4305警告,请在源文件中插入:

#pragma warning( disable : 4305)
在违规行之前

,然后是:

#pragma warning( default : 4305)

在违规行之后。 没有副作用。微软本可以这样做,可能。