如何在 Visual C 中抑制外部标头中的警告

发布于 2024-08-26 21:00:24 字数 192 浏览 6 评论 0原文

我正在启动一个新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

温柔戏命师 2024-09-02 21:00:24

在您无法或不想更改但需要包含的标头周围使用此方法。

您可以有选择地暂时禁用所有警告,如下所示:

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

您可以选择传入要禁用的警告编号,而不是 0,例如:

#pragma warning(push)
#pragma warning(disable : 4081)
#pragma warning(disable : 4706)
// Some code
#pragma warning(pop)

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:

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

Instead of 0 you can optionally pass in the warning number to disable, so something like:

#pragma warning(push)
#pragma warning(disable : 4081)
#pragma warning(disable : 4706)
// Some code
#pragma warning(pop)
·深蓝 2024-09-02 21:00:24

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.

椒妓 2024-09-02 21:00:24

/external:anglebrackets /external: W0 编译器标志 禁用对使用 #include <...> 导入的标头发出警告。

您可以将 W0 更改为 W1W2W3W4,为这些设置不同的警告级别。

The /external:anglebrackets /external:W0 compiler flags disable warnings on the headers imported with #include <...>.

You can change the W0 to W1, W2, W3, or W4, to set a different warning level for those.

桃扇骨 2024-09-02 21:00:24

我不相信 Visual C++ 能让你脱颖而出。您可以通过在包含周围使用 #pragma warning 来伪造它:

#pragma warning(push, 0)
#include "mywarningheader.h"
#pragma warning(pop)

I don't believe Visual C++ lets you differentiate. You can fake it by using #pragma warning around the include:

#pragma warning(push, 0)
#include "mywarningheader.h"
#pragma warning(pop)
春风十里 2024-09-02 21:00:24

似乎这个问题已经有了答案。

这篇文章讨论 /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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文