是否有 gcc/Xcode pragma 来抑制警告?
是否有一个#pragma让gcc/Xcode抑制特定的警告,类似于Java的@SuppressWarning注释?
我通常使用 -Wall
进行编译,但在某些情况下,我只想忽略特定的警告(例如,在编写一些快速/脏代码只是为了帮助调试某些内容时)。
我不是在寻找“修复代码”的答案。
Is there a #pragma
to have gcc/Xcode suppress specific warnings, similar to Java's @SuppressWarning
annotation?
I compile with -Wall
as a rule, but there are some situations where I'd like to just ignore a specific warning (e.g. while writing some quick/dirty code just to help debug something).
I'm not looking for "fix the code" answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个可行的解决方案 。使用#pragma GCC system_header 让 GCC 以非常特殊的方式处理您的代码,从而抑制任何非致命的
#warning
。请记住,您只是在欺骗预处理器,而不是真正的编译器。大多数时候抑制警告可能是有害的。
Here's a viable solution. Use
#pragma GCC system_header
to let GCC handle your code in a very special way, thus suppressing any non fatal#warning
.Remeber you're just fooling your preprocessor, not the real compiler. Suppressing warnings could be harmful most of times.
在 gcc4.6 及更高版本中,您可以使用 pragma 来抑制特定警告,并仅对特定代码块进行抑制,即:
push/pop 用于保留在处理代码之前就位的诊断选项。
这将是比使用“#pragma GCC system_header”来抑制所有警告更好的方法。 (当然,在较旧的 gcc 中,您可能会被
#pragma GCC system_header
方法“卡住”!)这里有一个关于抑制 gcc 警告的很好的参考:
http://www.dbp-consulting.com/tutorials/SuppressingGCCWarnings.html
本页还介绍了如何使用 -fdiagnostics-show-option 来查找控制特定警告的选项。
当然,正如其他人提到的,解决所有警告的根本原因通常比抑制它们要好得多!然而,有时这是不可能的。
In gcc4.6 and later you can use pragma's to suppress specific warnings and do that suppression only to a specific block of code, i.e. :
The push/pop are used to preserve the diagnostic options that were in place before your code was processed.
This would be a much better approach than using "
#pragma GCC system_header
" to suppress all warnings. (Of course, in older gcc you may be "stuck" with the#pragma GCC system_header
approach!)Here's a nice reference on suppressing gcc warnings:
http://www.dbp-consulting.com/tutorials/SuppressingGCCWarnings.html
This page also describes how to use
-fdiagnostics-show-option
to find out what option controls a particular warning.Of course, as others mention, it's generally far preferable to fix the root cause of all warnings than to suppress them! However, sometimes that is not possible.