How does the compiler detect duplicate definition across translation units
How does a compiler detect duplicate definition across translation unit. Suppose there were a extern const variable declaration in an header file.
If this header file was used in more than one translation unit - each having a separate definition - each TU object creation would be successful, however when the final executable is created the error is thrown.Is there a reference table created to account these duplication while linking each of these TU (during the creation of the executable)?
Any link on this topic开发者_开发问答 would be helpful.
Thanks in advance for your explanation.
Normally this would be detected by the linker, rather than the compiler. The linker can then either coalesce the variables (often required for sloppy C/C++ coding) or report an error.
Yes, the linker builds a list of unresolved external references and then eventually goes on to attempt to resolve them one by one.
精彩评论