如何在 Visual C 中抑制外部标头中的警告
我正在启动一个新的 BREW 项目,并且我想使用警告级别 4 (/W4) 进行编译,以保持应用程序代码的美观和干净。问题是 BREW 头文件本身不能用 /W4 干净地编译。
在 gcc 中,您可以使用 -I 和 -isystem 来区分应用程序头和系统头,然后默认情况下 gcc 不会在系统头中报告任何编译警告。 Visual C++ 中有等效的机制吗?
I'm starting a new BREW project, and I'd like to compile with Warning Level 4 (/W4) to keep the application code nice and clean. The problem is that the BREW headers themselves don't compile cleanly with /W4.
In gcc you can differentiate between application and system headers by using -I and -isystem, and then by default gcc doesn't report any compilation warnings in system headers. Is there an equivalent mechanism in Visual C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您无法或不想更改但需要包含的标头周围使用此方法。
您可以有选择地暂时禁用所有警告,如下所示:
您可以选择传入要禁用的警告编号,而不是
0
,例如:Use this method around (a) header(s) that you cannot or don't want to change, but which you need to include.
You can selectively, and temporarily disable all warnings like this:
Instead of
0
you can optionally pass in the warning number to disable, so something like:Visual C++ 团队刚刚添加了对外部标头中警告级别的支持。您可以在他们的博客文章中找到详细信息:损坏的警告理论。
本质上,它会自动执行此处建议建议手动执行的操作:在 #include 指令之前推送新的警告级别,并在 #include 指令之后立即弹出。还有额外的标志来指定外部标头的位置,标志来处理所有 <> 。包括作为外部的 #pragma system_header 以及 Clang 或 GCC 中不可用的功能(截至撰写本文时),以在用户代码中实例化模板时跨模板实例化堆栈查看外部标头中的警告。
除了该帖子下的评论之外,您还可以在 reddit 公告中找到一些有用的讨论对于那篇文章。
Visual C++ team has just added support for warning levels in external headers. You can find the details in their blog post: Broken Warnings Theory.
In essence it does automatically what the suggestions here were recommending to do manually: pushes new warning level right before #include directive and pops it up right after. There are additional flags to specify locations of external headers, flag to treat all <> includes as external, #pragma system_header and a feature not available in Clang or GCC (as of this writing) to see warnings in external headers across template instantiation stack when the template was instantiated in the user code.
Besides the comments under that post, you can also find some useful discussion in a reddit announcement for that post.
/external:anglebrackets /external: W0
编译器标志 禁用对使用#include <...>
导入的标头发出警告。您可以将
W0
更改为W1
、W2
、W3
或W4
,为这些设置不同的警告级别。The
/external:anglebrackets /external:W0
compiler flags disable warnings on the headers imported with#include <...>
.You can change the
W0
toW1
,W2
,W3
, orW4
, to set a different warning level for those.我不相信 Visual C++ 能让你脱颖而出。您可以通过在包含周围使用
#pragma warning
来伪造它:I don't believe Visual C++ lets you differentiate. You can fake it by using
#pragma warning
around the include:似乎这个问题已经有了答案。
这篇文章讨论
/external:I
可用于包含带有一组特殊警告的标题。我自己没有测试过,但博客文章是 2017 年的。
It seems like there is an answer to this.
this post talks about
/external:I
that can be used to include headers with a special set of warnings.I have not tested it myself, but the blog post is from 2017.