gpt4 book ai didi

c - 为什么 GCC 会针对错误的 printf 格式说明符显示重复警告?

转载 作者:太空狗 更新时间:2023-10-29 17:07:24 26 4
gpt4 key购买 nike

我很好奇为什么 GCC 在编译这个文件时向我显示两个相同的警告:

$ cat test.c 
#include <stdio.h>

int main (int argc, char const *argv[])
{
long foo = 0l;
printf("%i\n", foo);

return 0;
}
$ gcc-4.2 -Wall test.c
test.c: In function ‘main’:
test.c:6: warning: format ‘%i’ expects type ‘int’, but argument 2 has type ‘long int’
test.c:6: warning: format ‘%i’ expects type ‘int’, but argument 2 has type ‘long int’

有趣的是,Clang 还给出了两个警告:

$ clang test.c 
test.c:6:14: warning: conversion specifies type 'int' but the argument has type 'long' [-Wformat]
printf("%i\n", foo);
~^ ~~~
%ld
test.c:6:14: warning: conversion specifies type 'int' but the argument has type 'long' [-Wformat]
printf("%i\n", foo);
~^ ~~~
%ld
2 warnings generated.

有什么想法吗?


信息:

$ gcc-4.2 -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/gcc/gcc-5666.3~278/src/configure
--disable-checking --enable-werror --prefix=/usr --mandir=/share/man
--enable-languages=c,objc,c++,obj-c++
--program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib
--build=i686-apple-darwin11 --program-prefix=i686-apple-darwin11-
--host=x86_64-apple-darwin11 --target=i686-apple-darwin11
--with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

$ clang -v
Apple clang version 2.1 (tags/Apple/clang-163.7.1) (based on LLVM 3.0svn)
Target: x86_64-apple-darwin11.1.0
Thread model: posix

编辑:一些人提出的“多架构”假设听起来不错,但我不确定它是否正确。如果我使用 -arch 强制使用单一架构,我会收到两个警告。如果我指定 -arch x86_64 -arch i386,我会收到两组重复的警告!

$ gcc-4.2 -Wall -arch x86_64 test.c 
test.c: In function ‘main’:
test.c:6: warning: format ‘%i’ expects type ‘int’, but argument 2 has type ‘long int’
test.c:6: warning: format ‘%i’ expects type ‘int’, but argument 2 has type ‘long int’

$ gcc-4.2 -Wall -arch x86_64 -arch i386 test.c
test.c: In function ‘main’:
test.c:6: warning: format ‘%i’ expects type ‘int’, but argument 2 has type ‘long int’
test.c:6: warning: format ‘%i’ expects type ‘int’, but argument 2 has type ‘long int’
test.c: In function ‘main’:
test.c:6: warning: format ‘%i’ expects type ‘int’, but argument 2 has type ‘long int’
test.c:6: warning: format ‘%i’ expects type ‘int’, but argument 2 has type ‘long int’

编辑:我没有得到所有警告类型的欺骗。 -Wformat 是迄今为止我遇到的唯一一种。例如,如果我输入一个未使用的变量,我只会收到一个警告:

$ cat test.c 
#include <stdio.h>

int main (int argc, char const *argv[])
{
long foo = 0l;
long bar;
printf("%i\n", foo);

return 0;
}

$ gcc-4.2 -Wall test.c
test.c: In function ‘main’:
test.c:7: warning: format ‘%i’ expects type ‘int’, but argument 2 has type ‘long int’
test.c:7: warning: format ‘%i’ expects type ‘int’, but argument 2 has type ‘long int’
test.c:6: warning: unused variable ‘bar’

最佳答案

这是因为 Apple 的 stdio.h header 附加了一个 GCC format attribute到它的 printf()...

声明

(例如,参见 printf() here__printflike()here 的声明)

...但是 GCC(和 Clang,因为它试图与 GCC 非常兼容!)已经内置知识 printf() 是一个接受 printf 的函数 风格的参数。由于内置知识,您将收到一次警告,而由于显式属性,您将收到第二次警告。

您可以通过自己做同样的事情在其他平台(至少有几个版本的 GCC)上展示相同的行为:

extern int printf(const char *, ...) __attribute__((__format__ (__printf__, 1, 2)));

int main (int argc, char const *argv[])
{
long foo = 0l;
printf("%i\n", foo);

return 0;
}

关于c - 为什么 GCC 会针对错误的 printf 格式说明符显示重复警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7380880/

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