c - C 中的外部链接

标签 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

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

最佳答案

考虑两个函数:

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/

相关文章:

c++ - openMP 新手,有什么建议可以将以下代码与 openMP 并行吗?

c++ - 将 vs printf 放在以下代码中?

c - 如何在 Visual Studio Code 调试器中轻松显示指针的多个值?

c - 从通过读取系统调用读取的file.txt中获取字符串的子字符串

#else 预处理器指令后的注释

C:指向指针地址的指针,需要说明

c - 一次可以使用套接字发送和接收的最大数据大小?(TCP 套接字)

c++ - 将外部可访问方法添加到 activex dll (axvlc.dll)

c - "static char"与 C 中的 "char"

当您取消引用空指针时,您能证明为什么强制转换很重要吗?