Kyle A. answered 05/05/20
Senior Software Engineer Specializing in Systems Programming
Hi,
Linkage has to do with what are called translation units in C++. A symbol with internal linkage is only visible to code within its translation unit while a symbol with external linkage can be referenced from other translation units. In this case a translation unit usually is a .cpp file that gets compiled into an object file. If you declare a const variable in a cpp file, it will have internal linkage by default and the name is local to that file. Therefore you can have other const variables with the same name in other cpp files without any problems with multiple definitions for 1 symbol. Note this only applies if the variables are at global scope (not inside a function).
This also applies to free functions although functions have external linkage by default. If you are compiling a program consisting of 2 cpp files and they both define a function called PrintData you will get linker errors complaining about multiple definitions for the PrintData function.
If you mark the PrintData function with the static keyword, this will change the linkage to internal and you will no longer have these errors.
Hope this helps!