有选择地仅对部分翻译单元禁用 GCC 警告

发布于 2024-07-23 08:06:36 字数 505 浏览 7 评论 0原文

与此 MSVC 预处理器代码最接近的 GCC 等效项是什么?

#pragma warning( push )                    // Save the current warning state.
#pragma warning( disable : 4723 )          // C4723: potential divide by 0
// Code which would generate warning 4723.
#pragma warning( pop )                     // Restore warnings to previous state.

我们在通常包含的标头中有代码,但我们不希望为其生成特定警告。 但是,我们希望包含这些标头的文件继续生成该警告(如果项目启用了该警告)。

What's the closest GCC equivalent to this MSVC preprocessor code?

#pragma warning( push )                    // Save the current warning state.
#pragma warning( disable : 4723 )          // C4723: potential divide by 0
// Code which would generate warning 4723.
#pragma warning( pop )                     // Restore warnings to previous state.

We have code in commonly included headers which we do not want to generate a specific warning for. However, we want files which include those headers to continue to generate that warning (if the project has that warning enabled).

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

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

发布评论

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

评论(3

以可爱出名 2024-07-30 08:06:36

最接近的是 GCC 诊断编译指示#pragma GCC诊断[警告|错误|忽略]“-无论什么”。 它与您想要的不太接近,请参阅链接以获取详细信息和注意事项。

The closest thing is the GCC diagnostic pragma, #pragma GCC diagnostic [warning|error|ignored] "-Wwhatever". It isn't very close to what you want, and see the link for details and caveats.

水晶透心 2024-07-30 08:06:36

我也做过类似的事情。 对于第三方代码,我根本不想看到任何警告。 因此,我没有指定 -I/path/to/libfoo/include ,而是使用了 -isystem /path/to/libfoo/include 。 这使得编译器将这些头文件视为“系统头文件”以用于警告目的,并且只要您不启用 -Wsystem-headers ,您就基本上是安全的。 我仍然看到一些警告从那里泄漏出来,但它减少了大部分垃圾。

请注意,只有当您可以通过包含目录隔离有问题的代码时,这才会对您有所帮助。 如果它只是您自己项目的子集,或者与其他代码混合,那么您就不走运了。

I've done something similar. For third-party code, I didn't want to see any warnings at all. So, rather than specify -I/path/to/libfoo/include, I used -isystem /path/to/libfoo/include. This makes the compiler treat those header files as "system headers" for the purpose of warnings, and so long as you don't enable -Wsystem-headers, you're mostly safe. I've still seen a few warnings leak out of there, but it cuts down on most of the junk.

Note that this only helps you if you can isolate the offending code by include-directory. If it's just a subset of your own project, or intermixed with other code, you're out of luck.

伴梦长久 2024-07-30 08:06:36

这是对Matt Joiner 的答案的扩展。

如果您不想在代码中生成编译指示,可以使用 _Pragma 运算符

#ifdef __GNUC__
#  define DIAGNOSTIC_ERROR(w) _Pragma("GCC diagnostic error \"" w "\"")
#  define DIAGNOSTIC_IGNORE(w) _Pragma("GCC diagnostic ignore \"" w "\"")
#  define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
#  define DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
#endif
// (...)

DIAGNOSTIC_ERROR("-Wuninitialized")
foo(a); // Error

DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE("-Wuninitialized")
foo(a); // No error

DIAGNOSTIC_POP
foo(a); // Error

This is an expansion to Matt Joiner's answer.

If you don't want to spawn pragmas all over your code, you can use the _Pragma operator:

#ifdef __GNUC__
#  define DIAGNOSTIC_ERROR(w) _Pragma("GCC diagnostic error \"" w "\"")
#  define DIAGNOSTIC_IGNORE(w) _Pragma("GCC diagnostic ignore \"" w "\"")
#  define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
#  define DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
#endif
// (...)

DIAGNOSTIC_ERROR("-Wuninitialized")
foo(a); // Error

DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE("-Wuninitialized")
foo(a); // No error

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