如何在GCC中使用编译指示禁用所有警告 [英] How to disable all warnings using pragma directives in GCC
问题描述
我正在寻找一种方法来抑制所有可能通过pragma指令使用Gcc发出的警告.我制作了一些警卫宏,可帮助我使第三者标头从警告中消失,现在,它们就像msvc和clang的魅力一样工作.我仍然缺少使用Gcc诊断编译指示来抑制某个节中的每个警告的正确方法.让我给你举一些例子:
I am seeking for a way to suppress all possible warnings that i may get with Gcc with pragma directives. I had made some guard macros that help me silence 3rd party headers from warnings, and for now they work like charm for msvc and clang. I am still missing the correct way to use Gcc diagnostic pragmas in order to suppress every warning in a section. Let me give you some examples:
在msvc中,我们可以这样做:
In msvc we can do this:
#pragma warning(push, 0)
// Code that produces warnings...
#pragma warning(pop)
在clang中,我们可以这样做:
And in clang we can do this:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wall"
#pragma clang diagnostic ignored "-Wextra"
// Code that produces warnings...
#pragma clang diagnostic pop
中间的代码现在已从警告中完全消失了.
And the code that is in the middle is now being silenced from warnings for good.
在Gcc中,我们对clang也有类似的编译指示,我认为我可以尝试这样的事情:
And in Gcc we also have similar pragma directives with clang and i thought i could try something like this:
#pragma GCC diagnostic push
#pramga GCC diagnostic ignored "-Wall"
#pragma GCC diagnostic ignored "-Wextra"
// Code that produces warnings...
#pramga GCC diagnostic pop
但是在GCC中在诊断中忽略的杂注中传递-Wall和-Wextra不能像clang一样起作用,并且不会禁用所有可能的警告.而不是通过特定的警告来禁用作品:
But passing -Wall and -Wextra in diagnostic ignored pragma in GCC does not work like clang, and does not disable all the possible warnings. Instead of this passing a specific warning to disable works:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
void foo (int x) // No longer getting "unused parameter 'x'" warning
{
}
#pragma GCC diagnostic pop
因此,到目前为止,我唯一能想到的解决方法是列出所有GCC警告标志,并像上面一样使用它们.有没有更优雅的解决方案?如果没有,我可以在哪里获得完整的Gcc警告标志列表(最好在普通列表中)?
So the only workaround i can think so far is to make a long list with all the GCC warning flags and use them like above. Is there a more elegant solution? If not where i can get the complete Gcc warning flag list (favorably in a plain list)?
推荐答案
文档说:
目前只能控制警告(通常由"-W ..."控制),并且并非全部.使用
-fdiagnostics-show-option
确定哪些诊断是可控制的,以及由哪些选项控制.
At the moment only warnings (normally controlled by ‘-W...’) can be controlled, and not all of them. Use
-fdiagnostics-show-option
to determine which diagnostics are controllable and which option controls them.
这篇关于如何在GCC中使用编译指示禁用所有警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!