Translation Units


Input files to a C++ compiler are preprocessed. The definition of preprocessing may be found in the topic titled phases of preprocessing. Preprocessing performs:

  1. trigraph substitution,
  2. file inclusion,
  3. conditional compilation and
  4. macro substitution.

The output of the preprocessing phase is referred to as a translation unit. Typically, a translation unit is formed from a C++ source file which may include multiple header files (.h and .hpp files).

After preprocessing has taken place, a C++ compiler decomposes the intermediate text into tokens, which it syntactically analyses to produce the output object file (assuming no errors were encountered). Object files may be:

Library files (.lib) can contain the actual object file or an import record for code that resides in a dynamic link library. An import record consists of the name of the dynamic link library and either an ordinal number associated with a function or the name of a function or object. A library file is used by a linker to resolve references (even in the case of code residing in a dynamic link library).

Static vs Dynamic Linking

The linker resolves references to externally defined objects either statically (in the case of object libraries) or dynamically (in the case of dynamic link libraries). Static linking involves copying code (in object format) from a library to the output file. Dynamic linking involves placing a reference to code residing in a dynamic link library in the output file. For dynamic linking, the loader is responsible for resolving references at runtime. Dynamic linking results in smaller executables and more efficient use of memory (because a single copy of the code can be shared by multiple applications). Static linking may give an application standalone status.