Translation unit

from Wikipedia, the free encyclopedia

When programming in C / C ++ , the translation unit is the final input to the compiler from which the object file is created. It is the output of the preprocessor . Similarly, the final input to the preprocessor is referred to as the preprocessor translation unit.

Specifically, this means for a source file that header files are included (preprocessor translation unit), lines of code are ignored in the context of the conditional compilation and macros are replaced by associated instructions. It also follows from this that the C and C ++ compilers do not compile the actual source file, but the translation unit. In C ++, classes usually each form a translation unit.

Explanation

A C / C ++ program consists of source files that contain preprocessor directives in addition to the actual code . The preprocessor creates translation units from the source files.

The object files resulting from the translation unit can be linked , whereupon an executable file is often created.

The preprocessor translation unit is created by actually including the header files.

Note that preprocessors and thus also the concept of the translation unit are theoretically not limited to C / C ++.

Visibility area (scope)

Translation units form a scope , which is sometimes called a somewhat imprecise file scope in English .

Elements that are only visible within a translation unit have so-called internal linkage, while elements that are visible in the entire program have external linkage , i. H. they are taken into account by the linker .

structure

Most of the code in a project is saved in the source files with the file extension .c (or .cpp, .c++or .ccfor C ++ ).

In addition, the source files usually have a header file with the file extension .h (more rarely .hppand .hhin C ++).

Header files are also often included by other header files.

Individual evidence

  1. a b ISO / IEC 9899: TC3. (PDF; 3.6 MB) In: Open Standards. September 7, 2007, p. 21 , accessed on August 25, 2019 (English).