Jump to content

Translation unit (programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nbarth (talk | contribs) at 08:00, 3 May 2013 (Scope: typo). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In C programming language terminology, a translation unit is the ultimate input to a C compiler from which an object file gets generated.[1] In casual usage it is sometimes referred to as a compilation unit. A translation unit roughly consists of a source file after it has been processed by the C preprocessor, meaning that header files listed in #include directives are literally included, sections of code within #ifdef are included or not, and macros have been expanded.

Context

A C program consists of units called source files (or preprocessing files). When the C preprocessor expands a source file with all header files declared by #include directives, the result is a preprocessing translation unit. Further preprocessing translates the preprocessing translation unit into a translation unit. From a translation unit, the compiler generates an object file, which can be further processed and linked (possibly with other object files) to form an executable program.

Scope

Translation units define a scope, roughly file scope, and functioning similarly to module scope; in C terminology this is referred to a internal linkage, which is one of the two forms of linkage in C. Names (functions and variables) declared outside of a function block may be visible either only within a given translation unit, in which case they are said to have internal linkage – they are invisible to the linker – or may be visible to other object files, in which case they are said to have external linkage, and are visible to the linker.

C does not have a notion of modules. However, separate object files (and hence also the translation units used to produce object files) function similarly to separate modules, and if a source file does not include other source files, internal linkage (translation unit scope) may be thought of as "file scope, including all header files".

Code organization

The bulk of a project's code is typically held in files with a .c suffix (or .c++ or .cpp for C++, cpp used more conventionally). Files intended to be included typically have a .h ( .hpp for C++) suffix, and generally do not contain function or variable definitions to avoid name conflicts when headers are included in multiple source files, as is often the case. Header files can and often are included in other header files. It is standard practice for all .c files in a project to include at least one .h file.

See also

References