1

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:

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)? The quote in the post seems to allow that. In the example after the OP's quote it has:

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

and it says that the extern declaration has internal linkage. Why is there a difference when using variables and functions?

template boy
  • 10,230
  • 8
  • 61
  • 97
  • 1
    You may find this previous question helpful: [Can't understand the declaration #3 in the Example of \[basic.link\]/6 C++14](http://stackoverflow.com/q/29904114/1708801) – Shafik Yaghmour Jun 08 '15 at 17:14
  • *"but I'm not sure why"* As far as I can tell, *that* question is based on false premises: The Standard says the `extern int i;` in your question has *internal* linkage (my interpretation of §3.5p6). – dyp Jun 08 '15 at 17:21

1 Answers1

0

Because variable "i" has static storage. 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).

Also, you may find this discussion interesting: Understanding static storage class in C

Community
  • 1
  • 1
Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42
  • 1
    The only `static` variables with external linkage I know are static data members of classes. `static` local variables don't have linkage, `static` variables of namespace scope have internal linkage. – dyp Jun 08 '15 at 18:34
  • Take a look again on example explanation given at §3.5p6, the trick is two-fold. If particular static variable has a match with "extern" declaration, then it appears as external linkage. If no such "extern" declaration matched, that static variable will have no linkage. This is why I told that static storage is "quite special" as there are non-apparent tricks here. – Yury Schkatula Jun 08 '15 at 21:55
  • Please see §3.5p9 and [CWG 426](http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#426). The `extern int i;` (#3) does not refer to the same entity as `static int i;` (#1). – dyp Jun 08 '15 at 22:36
  • Why? Linker successfully resolves them to the same entity (otherwise it could throw a message about ill-formed program). – Yury Schkatula Jun 09 '15 at 11:42
  • 1
    You cannot deduce from the behaviour of any linker what the Standard specifies. Linkers can contain errors, and they can simply *not comply to the C++ Standard*. CWG 426 says the C++ Standards Committee thinks the current specification is defective. The current specification (as interpreted by the committee) says #1 and #3 should be different entities. The committee (the CWG) thinks this example should have Undefined Behaviour or be an error. – dyp Jun 09 '15 at 14:33