如何关闭C源代码中的指定警告?

发布时间:2024年01月04日

忽略某些警告的问题,也许GCC diagnostic pragmas可以提供帮助

GCC diagnostic pragmas:

6.57.10 Diagnostic Pragmas

GCC allows the user to selectively enable or disable certain types of diagnostics, and change the kind of the diagnostic. For example, a project's policy might require that all sources compile with?-Werror?but certain files might have exceptions allowing specific types of warnings. Or, a project might selectively enable diagnostics and treat them as errors depending on which preprocessor macros are defined.

#pragma GCC diagnostic?kind?option

Modifies the disposition of a diagnostic. Note that not all diagnostics are modifiable; 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.

kind?is `error' to treat this diagnostic as an error, `warning' to treat it like a warning (even if?-Werror?is in effect), or `ignored' if the diagnostic is to be ignored.?option?is a double quoted string which matches the command-line option.

          #pragma GCC diagnostic warning "-Wformat"
          #pragma GCC diagnostic error   "-Wformat"
          #pragma GCC diagnostic ignored "-Wformat"
     

Note that these pragmas override any command-line options. GCC keeps track of the location of each pragma, and issues diagnostics according to the state as of that point in the source file. Thus, pragmas occurring after a line do not affect diagnostics caused by that line.

#pragma GCC diagnostic push

#pragma GCC diagnostic pop

Causes GCC to remember the state of the diagnostics as of each?push, and restore to that point at each?pop. If a?pop?has no matching?push, the command line options are restored.

          #pragma GCC diagnostic error "-Wuninitialized"
            foo(a);			/* error is given for this one */
          #pragma GCC diagnostic push
          #pragma GCC diagnostic ignored "-Wuninitialized"
            foo(b);			/* no diagnostic for this one */
          #pragma GCC diagnostic pop
            foo(c);			/* error is given for this one */
          #pragma GCC diagnostic pop
            foo(d);			/* depends on command line options */
     

GCC also offers a simple mechanism for printing messages during compilation.

#pragma message?string

Prints?string?as a compiler message on compilation. The message is informational only, and is neither a compilation warning nor an error.

          #pragma message "Compiling " __FILE__ "..."
     

string?may be parenthesized, and is printed with location information. For example,

          #define DO_PRAGMA(x) _Pragma (#x)
          #define TODO(x) DO_PRAGMA(message ("TODO - " #x))
          
          TODO(Remember to fix this)
     

prints `/tmp/file.c:4: note: #pragma message: TODO - Remember to fix this'.

文章来源:https://blog.csdn.net/lyd0813/article/details/135394112
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。