简体   繁体   English

外部变量声明没有内部链接

[英]Extern variable declaration does not have internal linkage

I'm asking this question as a follow up from this post. 我问这个问题,从一个跟进这个职位。 They say that the extern block declaration has external linkage and not internal linkage, but I'm not sure why: 他们说extern块声明具有外部链接,而没有内部链接,但是我不确定为什么:

static int i = 0; // #1
void g() {
    extern int i; // #3 external linkage
}

Why doesn't the extern declaration take the linkage of i (internal linkage)? 为什么extern声明不采用i的链接(内部链接)? The quote in the post seems to allow that. 帖子中的报价似乎允许这样做。 In the example after the OP's quote it has: 在OP引用后的示例中,它具有:

static void f();
void g() {
    extern void f(); // internal linkage
    // ...
}

and it says that the extern declaration has internal linkage. 它说extern声明具有内部链接。 Why is there a difference when using variables and functions? 为什么使用变量和函数会有区别?

Because variable "i" has static storage. 因为变量“ i”具有静态存储。 So, in terms of your snippet, 因此,就您的摘录而言,

  • omitting the "static" statement will produce "no linkage" 省略“静态”语句将产生“无链接”
  • applying "static" statement will produce "external linkage" (as static storage is quite special part of the C/C++ runtime infrastructure). 应用“静态”语句将产生“外部链接”(因为静态存储是C / C ++运行时基础结构中非常特殊的一部分)。

Also, you may find this discussion interesting: Understanding static storage class in C 此外,您可能会发现此讨论很有趣: 了解C中的静态存储类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM