使用 GCC 有选择地删除警告消息 [英] Selectively remove a warning message using GCC

查看:23
本文介绍了使用 GCC 有选择地删除警告消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码:

Int32 status;
printf("status: %x", status)

给我以下警告:

jpegthread.c:157: warning: format '%x' expects type 'unsigned int', but argument 3 has type 'Int32'

我知道我可以通过转换类型来摆脱它,但是是否可以使用 GCC 编译器标志来摆脱那种特定类型的警告,并且仍然使用 -Wall?

I know I can get rid of it by casting the type, but is it possible with a GCC compiler flag to get rid of that particular type of warning, and still use -Wall?

推荐答案

如果您需要该代码可移植,那么您应该将参数强制转换为 unsigned int,因为 int 类型可能具有不同的在某些平台上比 Int32 大小.

If you need that code to work portable then you should cast the argument to unsigned int, as the int type may have a different size than Int32 on some platforms.

要回答有关在 GCC 中禁用特定警告的问题,您可以使用 -Wxxxx 在 GCC 中启用特定警告,并使用 -Wno-xxxx 禁用它们.

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx.

来自 GCC 警告选项:

您可以请求许多带有以 -W 开头的选项的特定警告,例如 -Wimplicit 请求对隐式声明的警告.这些特定警告选项中的每一个也有一个否定形式,以 -Wno- 开头以关闭警告;例如,-Wno-implicit.本手册仅列出了两种形式中的一种,以非默认形式为准.

You can request many specific warnings with options beginning -W, for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning -Wno- to turn off warnings; for example, -Wno-implicit. This manual lists only one of the two forms, whichever is not the default.

对于你的情况,有问题的警告是 -Wformat

For your case the warning in question is -Wformat

-Wformat

检查对 printf 和 scanf 等的调用,以确保提供的参数具有适合指定格式字符串的类型,并且格式字符串中指定的转换有意义.这包括标准函数和其他由格式属性指定的函数(请参阅函数属性),在 printf、scanf、strftime 和 strfmon(X/Open 扩展,不在 C 标准中)系列(或其他特定于目标的系列).在没有指定格式属性的情况下检查哪些函数取决于选择的标准版本,没有指定属性的函数检查由-ffreestanding-fno-builtin 禁用.

Check calls to printf and scanf, etc., to make sure that the arguments supplied have types appropriate to the format string specified, and that the conversions specified in the format string make sense. This includes standard functions, and others specified by format attributes (see Function Attributes), in the printf, scanf, strftime and strfmon (an X/Open extension, not in the C standard) families (or other target-specific families). Which functions are checked without format attributes having been specified depends on the standard version selected, and such checks of functions without the attribute specified are disabled by -ffreestanding or -fno-builtin.

根据 GNU libc 2.2 版支持的格式功能检查格式.这些包括所有 ISO C90 和 C99 特性,以及来自单一 Unix 规范和一些 BSD 和 GNU 扩展的特性.其他库实现可能不支持所有这些功能;GCC 不支持对超出特定库限制的功能发出警告.但是,如果 -pedantic-Wformat 一起使用,则会给出有关不在所选标准版本中的格式功能的警告(但不适用于 strfmon 格式,因为那些不在任何版本的 C 标准).请参阅控制 C 方言的选项.

The formats are checked against the format features supported by GNU libc version 2.2. These include all ISO C90 and C99 features, as well as features from the Single Unix Specification and some BSD and GNU extensions. Other library implementations may not support all these features; GCC does not support warning about features that go beyond a particular library's limitations. However, if -pedantic is used with -Wformat, warnings will be given about format features not in the selected standard version (but not for strfmon formats, since those are not in any version of the C standard). See Options Controlling C Dialect.

这篇关于使用 GCC 有选择地删除警告消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆