联动期间外部函数? [英] extern function during linkage?
问题描述
我有这种奇怪的事情:
在file1.c中有
extern void foo(int x, int y);
..
..
int tmp = foo(1,2);
在这个项目我能找到的只有这富():
in the project I could find only this foo():
在file2.c中:
int foo(int x, int y, int z)
{
....
}
在file2.h:
int foo(int x, int y, int z);
file2.h不包括在file1.c中(这就是为什么谁写的使用的extern,我猜)。
file2.h isn't included in file1.c (this is why who wrote it used extern, i guess).
该项目编译好,我想这是因为在file1.c中富()将被找只在联动,对吗?
this project compiles fine, I think that's because in file1.c foo() will be looked for only during linkage, am I right?
但我真正的问题是:为什么是联动succssful?
毕竟,有与2个参数富无此功能....
而我在C ..所以没有超载。
but my real question is : why is the linkage succssful ? after all, there is no such function as foo with 2 parameters.... and i'm in c .. so there's no overloading..
所以这是怎么回事?
推荐答案
由于没有超载,C编译器不装饰函数名。链接器在 file2.c中
找到一个参考的作用富
和 file1.c中
它找到一个函数富
。它可以不知道自己的参数列表不匹配,并愉快地使用它们。
Because there is no overloading, the C compiler does not decorate the function names. The linker finds in file2.c
a reference to function foo
and in file1.c
it finds a function foo
. It cannot know their parameter lists do not match and happily use them.
当然,当函数富
运行以Z
的值是垃圾和程序的行为变得未predictable从这一点上。
Of course, when the function foo
runs the value of z
is garbage and the behavior of the program becomes unpredictable from that point on.
这篇关于联动期间外部函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!