views:

29

answers:

2

Hi All,
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.

A: 

Yes, the linker builds a list of unresolved external references and then eventually goes on to attempt to resolve them one by one.

500 - Internal Server Error
A: 

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.

Paul R