How does the linker knows which symbol to which one link?
Say I have two .cpp files and in one of them I wrote
extern int i ;
and in another one I define the i variable.
Now how the linker knows that in the first file the i should be linked to the address of "i" in the second file? This question arises, because as I know, the object file does n开发者_开发技巧ot have any info about variable names (it knows only addresses) (see this link).
I am really confused in this.
Some light reading: Beginner's Guide to Linkers.
The object code has symbol definitions in it. The linker uses these to resolve references to symbols. The symbols are not part of the executable code, and cannot be read by code that is contained within the object file (hence the answer to the question to link to).
The linked executable may also have symbols in it (e.g. for use by a debugger), or may have symbols removed at link stage (or later) since they are of no use to the code contained within the executable.
精彩评论