gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:57:53 26 4
gpt4 key购买 nike

这段代码:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com