A "normal" VC++ project1 instruct VC++ to compile each source file by itself to an "object file" (or object module), where the compiler leaves "placeholders" for symbols that aren't resolved yet (like external
variables or functions that are declared but not defined).
Then, all the object files are tied together by the linker, which ties them together to produce the final executable. During this passage, the "placeholders" are replaced by the actual address of the code/data they refer to that is defined in the various object files. If some needed definition is not found you get an undefined reference error, if some symbol is found more than once you get a multiple definition error2.
Fore more information about the classical model for linking, have a look at this article by Raymond Chen (and, if you are interested, to the whole series).
- Surely there's room for more flexibility, here I'm just describing what's the usual situation in VC++.
- This is a consequence of the C++ "one definition rule"; still, there are some exceptions to this, in particular
inline
functions andtemplate
instantiations; in those cases, the linker just takes whatever it prefers (which shouldn't be a problem, since multiple definitions of such objects must be the same).