Skip to content
Search

Compilation process

lastmod 2018-08-17created 2017-02-012 edits

MSVC preprocessing translation phases

For the latest documentation on Visual Studio 2017 RC, see the Visual Studio 2017 RC documentation.

C and C++ programs consist of one or more source files, each of which contains some of the text of the program. A source file, together with its include files (files that are included using the #include preprocessor directive) but excluding sections of code removed by conditional-compilation directives such as #if, is called a "translation unit."

Source files can be translated in different time zones. Typically, only out-of-date files are translated. Translated translation units can be processed into separate object files or object-code libraries. These separately translated translation units are then linked to form an executable program or a dynamic-link library (DLL). For more information on files that can be used as input to the linker, see LINK input files.

Translation units can communicate using:

  • Calls to functions that have external linkage.
  • Calls to class member functions that have external linkage.
  • Direct modification of objects that have external linkage.
  • Direct modification of files.
  • Interprocess communication (Microsoft Windows-based applications only).

The following list describes the phases in which the compiler translates files.

  1. Character mapping: Characters in the source file are mapped to the internal source representation. Trigraph sequences are converted to single-character internal representation in this phase.
  2. Line splicing: All lines ending in a backslash () immediately followed by a newline character are joined with the next line in the source file, forming logical lines from the physical lines. Unless it is empty, a source file must end in a newline character that is not preceded by a backslash.
  3. Tokenization: The source file is broken into preprocessing tokens and white-space characters. Descriptions in the source file are each replaced by one space character. Newline characters are retained.
  4. Preprocessing: Preprocessing directives are executed and macros are expanded into the source file. The #include statement invokes translation starting with the preceding three translation steps on any included text.
  5. Character-set mapping: All character-set members and escape sequences are converted to their equivalents in the execution character set. For Microsoft C and C++, both the source and execution character sets are ASCII.
  6. String concatenation: All adjacent string and wide-string literals are concatenated. For example, "String " "concatenation" becomes "String concatenation".
  7. Translation: All tokens are analyzed syntactically and semantically; these tokens are converted into object code.
  8. Linkage: All external references are resolved to create an executable program or a dynamic-link library.

The compiler issues warnings or errors during the phases of translation in which it encounters syntax errors.

The linker resolves all external references and creates a DLL or executable program by combining one or more separately processed translation units along with standard libraries.

See also

Favorite site