MSVC - 停止标题中的警告
我正在将 MSVC 与 CMaked 项目一起使用。因此,我在 MSVC 上启用了许多为 gcc 和 clang 启用的标志。然而,/Wall 警告级别让我有些痛苦;它警告我包含头文件中的各种内容,例如 stdio.h 和 boost 头文件。有没有办法阻止 MSVC 就标头中的内容向我发出警告?我喜欢我的警告级别,但我只想为我启用它们。
I'm using MSVC with a CMaked project. As a result, I've enabled many of the flags on MSVC which were enabled for gcc and clang. However, the /Wall warning level is giving me some pain; it warns me about all kinds of things in included headers, like stdio.h and boost headers. Is there a way to stop MSVC from warning me about things in headers? I like my warning levels, but I only want them enabled for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
/wdXXXX
标志禁用特定警告,其中XXXX
是您希望忽略的警告数量。无需修改代码。You can disable specific warnings using the
/wdXXXX
flag whereXXXX
is the number of the warnings you wish to ignore. No need to modify the code.长期以来,MSVC 缺乏与 GCC/Clang 不同的功能来正确处理这种情况。
现在有一个解决方案。
本质上,这是 MSVC 的
-isystem
版本,长期以来一直是GCC
/Clang
的一项功能。尽管如此。鉴于此功能的新颖性,库编写者仍应确保其面向公众的头文件在 MSVC 上尽可能干净地编译。
许多用户可能无法更新他们的 VS 版本,或者不知道这个新功能。
链接:
原文博文:
https://devblogs.microsoft .com/cppblog/customized-warning-levels-and-code-analysis-for-external-headers/
官方文档:
https:// learn.microsoft.com/en-us/cpp/build/reference/external-external-headers-diagnostics?view=msvc-170
For a long time MSVC lacked the functionality to properly deal with this situation unlike GCC/Clang.
Now there is a solution for this.
Essentially this is MSVC's version of
-isystem
which has long been a feature ofGCC
/Clang
.Still though. Given how new this feature is, library writers should still make sure their public facing header files compile as cleanly as possible on MSVC.
Many users may not be able to update their VS version, or won't be aware of this new functionality.
Links:
Original blog post:
https://devblogs.microsoft.com/cppblog/customized-warning-levels-and-code-analysis-for-external-headers/
Official documentation:
https://learn.microsoft.com/en-us/cpp/build/reference/external-external-headers-diagnostics?view=msvc-170
Mark Tolonen 已经指出了
/W4
。如果仍然产生警告,例如您正在使用旧的 MSVC 版本(如 7.1),或者您正在使用某些仍然会产生有关完美代码的警告的第 3 方库,并且您的目标是干净编译,然后查看我的 msvc 愚蠢警告抑制 标头。
它已经在 comp.lang.c++ Usenet 组中经过了几轮社区审查,但它可能/将需要更新,因为微软在新的编译器版本中添加了更多愚蠢的警告......;-)
Mark Tolonen has already point out
/W4
.If that still produces warnings, e.g. you're using an older MSVC version like 7.1, or you're using some 3rd party library that still produces warnings about perfectly good code, and you're aiming for clean compiles, then see my msvc silly-warning suppression header.
It's been through a few rounds of community review, in the comp.lang.c++ Usenet group, but it may/will need updating as Microsoft adds even more silly-warnings in new compiler versions… ;-)
/Wall
非常迂腐。/W4
可能就是您真正需要的。要回答您的问题,您可以通过以下方式禁用标头周围的特定警告:或者通过以下方式更改警告级别:
请参阅 MSDN 文档:http://msdn.microsoft.com/en-us/library/2c8f766e.aspx
/Wall
is very pedantic./W4
is probably all you really need. To answer your question, you can disable specific warnings around your headers with:Or change the warning level with:
See the MSDN documentation: http://msdn.microsoft.com/en-us/library/2c8f766e.aspx
您可以对项目“外部”标头使用不同的警告级别:
请参阅 /external(外部标头诊断) 了解详细信息。
You can use a different warning level for headers "external" to your project :
See /external (External headers diagnostics) for details.