What happens during preprocessing, compilation, and linking, and why can a translation unit without main compile while a hosted executable fails to link?
Show answer and explanation
Conceptually, preprocessing resolves directives and produces the token stream for a translation unit. Compilation performs parsing, semantic checks, optimization, and usually emits an object file containing code, data, symbols, and unresolved references. Linking combines object files and libraries, resolves those references, applies relocation, and creates the requested executable or library. A source file is only one translation unit, so it can compile to an object without defining main; it may be a library component whose declarations are completed elsewhere. A hosted C++ program needs one valid global main definition, and the executable link commonly reports a missing entry point or unresolved main when none is supplied. Multiple external definitions instead produce an ODR violation and commonly a duplicate-symbol diagnostic. Freestanding implementations and platform startup rules can use a different entry contract, so distinguish the C++ hosted-program rule from an operating system's low-level entry symbol.
Conceptually, preprocessing resolves directives and produces the token stream for a translation unit. Compilation performs parsing, semantic checks, optimization, and usually emits an object file containing code, data, symbols, and unresolved references. Linking combines object files and libraries, resolves those references, applies relocation, and creates the requested executable or library. A source file is only one translation unit, so it can compile to an object without defining main; it may be a library component whose declarations are completed elsewhere. A hosted C++ program needs one valid global main definition, and the executable link commonly reports a missing entry point or unresolved main when none is supplied. Multiple external definitions instead produce an ODR violation and commonly a duplicate-symbol diagnostic. Freestanding implementations and platform startup rules can use a different entry contract, so distinguish the C++ hosted-program rule from an operating system's low-level entry symbol.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.