0

Why does

int main(){
  int x; // no linkage
  extern int x; 
  return 0;
}

return the "extern follows non-extern" error? Since the second declaration is in the same scope as the first, and the first has no extern storage-class (thus no linkage by 6.2.2.6 in the C standard), shouldn't the the second declaration cause x to have external linkage by 6.2.2.4?

GuPe
  • 277
  • 2
  • 9
  • 1
    Is there any point of doing this or are you just curious? – klutt Nov 14 '20 at 22:04
  • 1
    Does this answer your question? [error: extern declaration of 'i' follows declaration with no linkage](https://stackoverflow.com/questions/45724358/error-extern-declaration-of-i-follows-declaration-with-no-linkage) – yflelion Nov 14 '20 at 22:06
  • @klutt I am just curious. I was wondering if this might be because no linkage identifiers must refer to unique entities as in 6.2.2.2 but I am not sure. – GuPe Nov 14 '20 at 22:07
  • @GuachoPerez `thus no linkage` This is your answer right there. The "extern" declaration directly conflicts with the "no linkage" premise, because "no linkage" is mutually exclusive with "external linkage". – dxiv Nov 14 '20 at 22:07
  • @dxiv I get it. So any non-`extern` declaration in block scope would have no linkage and adding external linkage would conflict with that? – GuPe Nov 14 '20 at 22:14
  • @GuachoPerez Right, [6.6.2](https://eel.is/c++draft/basic.link#2) makes it clear that a name with no linkage can have none of the other internal/module/external linkage types. – dxiv Nov 14 '20 at 22:16
  • @dxiv Thank you. I guess I just misinterpreted the "specifies no linkage" in 6.2.2.4 as specifying "no linkage" as opposed to not specifying either internal or external linkage. – GuPe Nov 14 '20 at 22:18

0 Answers0