gcc - 使用 GCC 有选择地删除警告消息

标签 gcc warnings

这段代码:

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

给我以下警告:

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

我知道我可以通过转换类型来消除它,但是是否可以使用 GCC 编译器标志来消除该特定类型的警告,并仍然使用 -Wall

最佳答案

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

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

来自GCC Warning Options :

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

-Wformat

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

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 - 使用 GCC 有选择地删除警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/925179/

相关文章:

macos - 禁用汇编器警告 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions"

python - 在被导入模块禁用后,在 Python 中启用所有警告

ios - 将 int 转换为 *NSInteger

gcc - gcc中的-lm是什么意思?

c++ - 为什么我应该始终启用编译器警告?

c++ - 为什么 visual studio 2017 坚持认为我在其他地方没有打开的文件在编辑器之外被修改了?

c - 无法识别的选项“-tree-vectorize”

c++ - -DXXX 编译器参数的 CMAKE 选项

c++ - Boost 1.65.1 中可能存在错误。在运行 bootstrap.bat gcc 时

c - 为什么在数组界限之外访问索引时,我的代码为什么不分段?