有没有办法用编译指示禁用所有警告?

发布于 2024-07-13 20:49:12 字数 1315 浏览 5 评论 0原文

我已经开始了一个新项目,并决定确保它在启用 /Wall 选项的情况下干净地构建。 唯一的问题是并非所有第 3 方库(如 boost)都在没有警告的情况下进行编译,因此我不得不在共享标头中执行此操作:

#pragma warning(push)

#pragma warning(disable:4820)
#pragma warning(disable:4619)
#pragma warning(disable:4668)
#pragma warning(disable:4625)
#pragma warning(disable:4626)
#pragma warning(disable:4571)
#pragma warning(disable:4347)
#pragma warning(disable:4640)
#pragma warning(disable:4365)
#pragma warning(disable:4710)
#pragma warning(disable:4820)
#pragma warning(disable:4350)
#pragma warning(disable:4686)
#pragma warning(disable:4711)
#pragma warning(disable:4548)

#include <boost/array.hpp>
#include <boost/assert.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/function.hpp>
#include <boost/integer.hpp>
#include <boost/optional.hpp>
#include <boost/regex.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/utility.hpp>
#include <boost/variant.hpp>

#pragma warning(pop)

这工作得很好,但每次我添加新的 boost 标头时,我都必须添加它们的任何警告生成到列表中。 有没有办法禁用这段代码的所有警告?

I've started a new project and have decided to make sure it builds cleanly with the /Wall option enabled. The only problem is not all 3rd party libraries (like boost) compile without warnings, so I've resorted to doing this in a shared header:

#pragma warning(push)

#pragma warning(disable:4820)
#pragma warning(disable:4619)
#pragma warning(disable:4668)
#pragma warning(disable:4625)
#pragma warning(disable:4626)
#pragma warning(disable:4571)
#pragma warning(disable:4347)
#pragma warning(disable:4640)
#pragma warning(disable:4365)
#pragma warning(disable:4710)
#pragma warning(disable:4820)
#pragma warning(disable:4350)
#pragma warning(disable:4686)
#pragma warning(disable:4711)
#pragma warning(disable:4548)

#include <boost/array.hpp>
#include <boost/assert.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/function.hpp>
#include <boost/integer.hpp>
#include <boost/optional.hpp>
#include <boost/regex.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/utility.hpp>
#include <boost/variant.hpp>

#pragma warning(pop)

This works well enough, but every time I add new boost headers I have to add whatever warnings they generate to the list. Is there a way to say disable all warnings for this stretch of code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

青衫负雪 2024-07-20 20:49:12

您可以推送/弹出低级别的警告,如下所示:

#pragma warning(push, 0)        

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
// ...

#pragma warning(pop)

但请注意,不可能禁用所有警告。 例如,某些链接器警告无法关闭。

You can push/pop a low level of warning, like this:

#pragma warning(push, 0)        

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
// ...

#pragma warning(pop)

But know that it's not possible to disable all warnings. For example, some linker warnings are impossible to turn off.

烟燃烟灭 2024-07-20 20:49:12
#pragma warning(disable:4820)
#pragma warning(disable:4619)
#pragma warning(disable:4668)

为了减少线路....

#pragma warning (disable : 4820 4619 4668)
#pragma warning(disable:4820)
#pragma warning(disable:4619)
#pragma warning(disable:4668)

for less lines....

#pragma warning (disable : 4820 4619 4668)
如痴如狂 2024-07-20 20:49:12

我之前所做的是设置“W3”选项而不是“Wall”,然后在我自己的每个源 .cpp 文件中,将其放在

#pragma warning(push, 4)

所有“#include ...”行之后的顶部,然后

#pragma warning(pop)

作为最后一个文件的行。

这样,您将在代码中收到 4 级警告,在第 3 方代码中收到 3 级警告,而您对此无能为力。

What I've done before is set the "W3" option rather than "Wall" then in each of my own source .cpp files I put

#pragma warning(push, 4)

at the top AFTER all the "#include..." lines and then

#pragma warning(pop)

as the very last line of the file.

This way you get level 4 warnings in your code and level 3 in 3rd party code that you can't do anything about.

轻许诺言 2024-07-20 20:49:12

使用 Assaf Lavie 的回答中描述的技术可以创建辅助宏:

#define DISABLE_ALL_WARNINGS_BEGIN \
    __pragma(warning(push, 0))

#define DISABLE_ALL_WARNINGS_END \
    __pragma(warning(pop))

它们可以按以下方式使用( 在线演示):

DISABLE_ALL_WARNINGS_BEGIN
void foo(int a)
{
    // this function should generate the following warning
    // "warning C4100: 'a': unreferenced formal parameter"
    // unless it is wrapped inside "DISABLE_ALL_WARNINGS" section
}
DISABLE_ALL_WARNINGS_END

Using the technique described in the Assaf Lavie's answer it is possible to create helper macros:

#define DISABLE_ALL_WARNINGS_BEGIN \
    __pragma(warning(push, 0))

#define DISABLE_ALL_WARNINGS_END \
    __pragma(warning(pop))

They can be used in the following way (online demo):

DISABLE_ALL_WARNINGS_BEGIN
void foo(int a)
{
    // this function should generate the following warning
    // "warning C4100: 'a': unreferenced formal parameter"
    // unless it is wrapped inside "DISABLE_ALL_WARNINGS" section
}
DISABLE_ALL_WARNINGS_END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文