Microsoft C 编译器 (cl.exe):可以限制每个文件 (/Wall) 的警告范围吗?
运行cl.exe
时,您可以指定警告级别。
cl /W3
cl /W4 # warn even more
cl /Wall # all warnings on
但是,最高级别 /Wall
似乎不切实际,因为它会触发 Windows 头文件中的警告,例如 windef.h
(适用于 VS2010 的 Windows SDK)。最常出现的两个似乎是 C4668 和 C4820。所以你可以关闭它们:
cl /Wall /wd4668 /wd4820
仍然给你留下 C4255:
C:\SDKs\Windows\v7.0A\include\windef.h(230) : warning C4255: 'FARPROC'
C:\SDKs\Windows\v7.0A\include\windef.h(231) : warning C4255: 'NEARPROC'
C:\SDKs\Windows\v7.0A\include\windef.h(232) : warning C4255: 'PROC'
所以你也补充道:
cl /Wall /wd4668 /wd4820 /wd4255
但其他的可能会突然出现。我可能想为我自己的代码保留这些警告,只是不要让输出因并非源自我的代码的警告而混乱。
有没有办法让编译器对标准头应用与我自己的代码不同的设置?
更新
嗯,有一个类似的问题,答案是/ W4
而不是 /Wall
。也许 MSVC 无法为不同的文件指定不同的设置。
When running cl.exe
, you can specify the warning level.
cl /W3
cl /W4 # warn even more
cl /Wall # all warnings on
However, the highest level, /Wall
, seems impractical because it triggers warnings in Windows header files, such as in windef.h
(Windows SDK for VS2010). The two most frequently occurring ones seem to be C4668 and C4820. So you can switch them off:
cl /Wall /wd4668 /wd4820
Still leaves you with C4255:
C:\SDKs\Windows\v7.0A\include\windef.h(230) : warning C4255: 'FARPROC'
C:\SDKs\Windows\v7.0A\include\windef.h(231) : warning C4255: 'NEARPROC'
C:\SDKs\Windows\v7.0A\include\windef.h(232) : warning C4255: 'PROC'
So you add that as well:
cl /Wall /wd4668 /wd4820 /wd4255
But others might crop up. And I might want to keep those warnings for my own code, just not have the output cluttered by warnings which did not originate in my code.
Is there a way to make the compiler apply different settings to standard headers than to my own code?
Update
Hmm, there's a similar question, and the answer is to go with /W4
instead of /Wall
. Maybe it's not possible with MSVC to specify different settings for different files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,Visual Studio 似乎没有选项来指定在特定搜索路径中找到的所有头文件的警告级别或类似的选项来关闭警告。由于您所描述的确切问题,我本人只是坚持使用
/W4
。我能想到的解决这个问题的唯一方法是在所有文件中包含有问题的标头的地方使用以下内容:
请注意,我实际上还没有尝试过这个,所以它可能根本不起作用!
Unfortunately, Visual Studio doesn't seem to have an option to specify warning levels for all header files found in a specific search path or something similar to turn off the warnings. I myself just stick with
/W4
because of the exact problem you're describing.The only way I can think of to get around this is to use the following in all your files wherever the offending headers are included:
Note that I haven't actually tried this, so it may not work at all!