How do I disable a gcc warning which has no command line switch?

不羁岁月 提交于 2021-02-07 11:44:05

问题


I get the following warning:

warning: 'X' is initialized and declared 'extern'

and it looks like it's no big deal and I could disable it. Changing the code is not really a good idea in my case because I have no control over the code, I just need to compile it. So I want to disable the warning.

If it had a -WSomeDefect key next to it then I could use a -Wno-SomeDefect command line switch but it looks like there's no distinct switch for this warning.

How do I disable such warning?


回答1:


For a direct answer to the posed question:

As can be seen from GCC's source code, there is no (semi-)specific switch to disable this warning. It seems to only be disabled by disabling all warning (-w) or including the offending code as system header via -isystem, both of which are non-specific in the suppressed warnings.

There is a related open bug report on GCC here and an open meta-bug for similar cases of warnings without switches here.

If you don't want to use the two non-specific suppression mechanisms, then you probably will have to patch in an additional flag to GCC or wait for the bugs to be worked on in order to disable this specific warning.




回答2:


There're hundreds of instances of this warning and they flood the compiler output. Hard to ignore.

If this is external library there is way to reduce this warning to single warring report. I'm suspecting that you can live with single warning message.

Wrap this library API with your own functions/methods. You can name them 1:1 using different namespace to avoid complex modification of own code where this API is used. This way this warning will be reported only when source including problematic header file is included. Aim is to include problematic header file only once.

Depending how this API looks like it may be harder to do.

Anyway if this is third party library then this approach will make it easier to mock that library and write test for your code.




回答3:


There isn't any specific flag to disable this, which is unfortunate, but there is the -w flag that will disable all warnings, with no way to re-enable them.

From the GCC docs:

-w
Inhibit all warning messages.

But the better option here is to remove the extern from the variable declarations/definitions. That way, the compiler knows the variable is defined within the translation unit. You can contact the owners of the code to see if they can change it.




回答4:


The code producing this warning is not valid C and should be fixed. The C language requires issuing "diagnostics" (warnings or errors) for constraint violations such as this, and does not mandate the existence of any way to disable them. I believed (and I suspect many others do) that extern was a constraint violation with an initializer, since in normal usage extern only provides a declaration, not a definition. However, per 6.9.2 ¶1:

If the declaration of an identifier for an object has file scope and an initializer, the declaration is an external definition for the identifier.

Being that the grammar seems to allow extern with an initializer, this is indeed a valid definition.

GCC has a -w option that will probably shut the warning off, but at the expense of disabling all warnings with no ability to override and turn some back on. This would make sense if it were a constraint violation and would be a sign to fix the invalid code; however, the code is valid and GCC absolutely should be providing a mechanism not to produce a spurious warning about it.

As noted by Eljay in a comment:

Warnings come in several categories: by the standard required diagnostic message; lint-like static analysis of common accidental language abuse/misuse; well-meaning but still stylistic opinion (e.g., -Weffc++); possibly too pedantic and/or minutia (e.g. -Weverything or -pedantic). The latter categories ought to have "opt-out" ways to disable the specific warning, such as in the OP's case.

GCC generally tries to honor this ought, most of the time, and I think the absence of a way to disable this one would be worth reporting to the GCC bug tracker.



来源:https://stackoverflow.com/questions/57957168/how-do-i-disable-a-gcc-warning-which-has-no-command-line-switch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!