[英]Calling implicitly declared function with too few arguments: why there is no linker error?
例子:
/* lib.c */
#include <stdio.h>
int f1(int a)
{
printf("%d\n", a);
return 0;
}
/* t3.c */
int main()
{
f1();
return 0;
}
gcc -c -o lib.o lib.c && gcc t3.c lib.o && ./a.exe
t3.c: In function 'main':
t3.c:3:2: warning: implicit declaration of function 'f1' [-Wimplicit-function-declaration]
3 | f1();
| ^~
1
clang -c -o lib.o lib.c && clang t3.c lib.o && ./a.exe
t3.c:3:2: warning: implicit declaration of function 'f1' is invalid in C99 [-Wimplicit-function-declaration]
f1();
^
1 warning generated.
1
问题:
a.exe
)?error: too few arguments to function call f1, expected 1, have 0
?为什么这甚至编译(生成ieaexe)?
编译器支持向后兼容。 在 C99 标准之前,可以在不提供声明的情况下调用函数。 C 编译器不会像 C++ 编译器那样为函数生成重整名称。
为什么没有像错误这样的链接器错误:函数调用 f1 的参数太少,预期为 1,有 0?
链接器找到了外部名称 f1。 因此对 f1 的引用已解决。 链接器不检查函数的调用方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.