有选择地仅对部分翻译单元禁用 GCC 警告
与此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最接近的是 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.我也做过类似的事情。 对于第三方代码,我根本不想看到任何警告。 因此,我没有指定 -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.
这是对Matt Joiner 的答案的扩展。
如果您不想在代码中生成编译指示,可以使用 _Pragma 运算符:
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: