there is no other reason of extern linkage existence? |
Is it possible that you are mixing up syntax and concepts?
External linkage itself sometimes can’t be avoided – or, let’s say, it’s a C legacy that was considered important enough to be kept in C++.
The syntax used to achieve external linkage involves the keyword “extern”.
So: if a variable is defined out of every function in a cpp file, it has global scope and external linkage, in the meaning that offers you the possibility to link to it from another file.
But if you want to link to it, you are need to use the keyword “extern”.
The reason is, I think, rather obvious, since you are not expected to know the code inside other cpp files, which could (and often are) already compiled; therefore you could decide to declare another variable with the same name in your file without being aware of what other programmers have done.
Let me explain by examples:
Mr. John Smith writes a cpp file:
1 2 3 4
|
#include...
[...]
int john_global_variable = 666;
[...]
|
Mrs. Ann Johnsons, who isn’t even aware of Mr. Smith existence, writes her own cpp file:
1 2 3 4
|
#include...
[...]
int ann_global_variable = 13;
[...]
|
I download there
already compiled libraries from Internet and decide to use, for my global variables, these names:
1 2 3 4 5
|
#include...
[...]
int john_global_variable;
int ann_global_variable;
[...]
|
My code will run smoothly until I decide to take advantage of their (my two variables) external linkage by adding, in another file:
1 2
|
extern int john_global_variable; // crash! Which one? Enoizat’s or John Smith’s?
extern int ann_global_variable; // crash! Which one? Enoizat’s or Ann Johnsons’s?
|
How you can see:
- external linkage is consider useful by Stroustrup & C(++)
- variables declared at file-level scope have (=offer!) external linkage
- to take advantage of external linkage you need to explicitly state “extern”, otherwise your code could conflict with the one written by other, of which existence you could not even being aware.
If that’s not what you were asking... well, I’m afraid I must give up, because I can’t understand the question.