Sim*_*ker 7 c gcc compilation clang
我很好奇为什么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’
Run Code Online (Sandbox Code Playgroud)
有趣的是,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.
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
有关信息:
$ 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
Run Code Online (Sandbox Code Playgroud)
编辑:"多架构"假设有些人认为听起来不错,但我不确定它是对的.如果我强制使用单一架构-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’
Run Code Online (Sandbox Code Playgroud)
编辑:我没有得到所有警告类型的欺骗.-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’
Run Code Online (Sandbox Code Playgroud)
这是因为Apple的stdio.h标题将GCC format属性附加到其声明printf()......
(例如,见的声明printf() 在这里和申报__printflike()宏观这里)
...但是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;
}
Run Code Online (Sandbox Code Playgroud)