0

So I read that:

An identifier’s linkage determines whether other declarations of that name refer to the same object or not.

source

and that variables especially local ones:

have no linkage

int main()
{
     int x;
     {
         int x;
     }
}
  • Concerning the fact that local variables have no linkage is it because when we declare a variable it also defines it, and so both x declarations will never refer to the same object(or else naming collision will occur)?
  • Can a local variable have linkage if it is specified as extern?

Thanks in advance.

Alex
  • 840
  • 7
  • 23
  • Only local variables have no linkage. global variables with `static` have internal linkage, and with `extern` have external linkage. – Waqar Jun 27 '20 at 10:13
  • Does this answer your question? [Difference between internal and no linkage](https://stackoverflow.com/questions/24864840/difference-between-internal-and-no-linkage) – Waqar Jun 27 '20 at 10:15
  • No, it is because x has no existence out of this scope, it cannot be linked to other translation units and it is just valid in that small local scope. Linkage comes in when different translation units needto talk to eachother . – Waqar Jun 27 '20 at 10:23
  • Local variables have no linkage *because they are local variables.* Your further questions don't make sense. – user207421 Jun 27 '20 at 10:24
  • @MarquisofLorne I don't understand why _because_ they are local variables they don't have linkage, is it because when the scope ends, the variable won't exist? – Alex Jun 27 '20 at 10:25
  • @Alex Because that's what it says in the language specification that you quoted from, or that your (uncited) sources were referring to. And 'A local variable defined as `extern`' is a contradiction in terms – user207421 Jun 27 '20 at 10:26
  • @MarquisofLorne Well I want to know why, that's the whole point of my question – Alex Jun 27 '20 at 10:26
  • Local variables are declared in a local scope and are not exposed to linkage, i.e. the linker in any way. The explanation tends to get circular, as that in turn is what 'local scope' means, and on and on. Ultimately it has to come down to what it says in the language specification. – user207421 Jun 27 '20 at 10:28
  • @MarquisofLorne the thing I don't get is `whether other declarations of that name refer to the same object or not.` why can't that be applied to _local variables_? – Alex Jun 27 '20 at 10:30
  • Other declarations are distinct by definition. – user207421 Jun 27 '20 at 10:37
  • 2
    A local variable cannot have any linkage, because it is not a fixed object. Local variables only exist while that function is running. Therefore, local variables may not exist at all at a certain time, or they even may exist several times at once in the case of a [recursive function](https://en.wikipedia.org/wiki/Recursion_(computer_science)). – Andreas Wenzel Jun 28 '20 at 02:17
  • @AndreasWenzel Thank you, would you be able to formulate that in an answer and I'll accept it :D – Alex Jun 28 '20 at 07:57
  • 2
    That's all true but it still isn't the basic reason. The basic reason is that local variables are declared in scopes, which is what makes them local, by definition. – user207421 Jun 28 '20 at 09:48
  • I agree with @MarquisofLorne that my comment is not a full answer, therefore I will not post it as an answer. Thanks for offering to accept it, though. – Andreas Wenzel Jun 28 '20 at 23:19
  • You are of course welcome to formulate your own answer based on the comments, if you want your question to have an answer. See the following official help page for more information: [Can I answer my own question?](https://stackoverflow.com/help/self-answer) – Andreas Wenzel Jun 29 '20 at 15:48

1 Answers1

0

Based on the comments I received, I formulated the following answer:

  • Local variables have a scope which means that they don't exist out of this scope, thus are not exposed to linkage.
  • Local variables cannot be exposed to translation units, contradicting the definition of linkage.
  • Local variables are not fixed because of their limited lifetime. Thus are unstable and not suited for linkage.
Alex
  • 840
  • 7
  • 23