gpt4 book ai didi

c - C 中的外部链接

转载 作者:太空狗 更新时间:2023-10-29 15:03:04 25 4
gpt4 key购买 nike

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

请解释一下这是什么意思,我不明白

最佳答案

考虑两个函数:

extern int extern_sqr(int i) { return i * i; }
static int static_dbl(int i) { return i * 2; }

然后引用 extern_sqr 的人将引用该函数。这与 static 链接相反,只有来自“翻译单元”(大致是它定义的文件)内的人才能访问函数 static_dbl

事实证明,extern 在 c 中是默认隐含的。所以,如果你这样写,你会得到同样的行为:

int extern_sqr(int i) { return i * i; }

较新的 C 标准仍然需要“函数声明”,因此,通常在某个地方的头文件中,您会遇到:

int extern_sqr(int i);  // Note: 'i' is optional

上面写着“在某个地方,在其他一些翻译单元中,我有一个名为 extern_sqr 的函数。

同样的逻辑也适用于变量。

关于c - C 中的外部链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3093558/

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