在C外部链接 [英] External linkage in C
问题描述
K&安培; R说:
在默认情况下外部变量和函数有同名,他们的所有引用甚至从功能单独编译的财产,都是同样的事情引用
by default external variables and functions have the property that all references to them by the same name, even from functions compiled separately, are references to same thing
请解释一下这是什么意思,我不明白
Please explain what this means, I don't understand it
推荐答案
考虑两个功能:
extern int extern_sqr(int i) { return i * i; }
static int static_dbl(int i) { return i * 2; }
那么谁参照 extern_sqr
人会指的是功能。这是反对静态
联动,其中从翻译单位(大概它定义的文件)中的人才能访问功能 static_dbl
。
Then people who refer to extern_sqr
will be referring to that function. This is opposed to static
linkage, where only people from within the "translation unit" (roughly the file it's defined) can access the function static_dbl
.
原来,该的extern
默认情况下,在C暗示。所以,你会得到相同的行为,如果你写:
It turns out, that the extern
is implied by default in c. So, you would get the same behavior, if you wrote:
int extern_sqr(int i) { return i * i; }
较新的C类标准仍然需要一个函数声明,因此,通常在头文件中的某个地方,你会遇到:
Newer C standards still require a "function declaration" so, usually in a header file somewhere, you'll encounter:
int extern_sqr(int i); // Note: 'i' is optional
它说的地方,在其他一些翻译单元,我有一个调用的函数 extern_sqr
。
同样的逻辑也适用于变量。
The same logic applies to variables.
这篇关于在C外部链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!