In C and C++ programming language terminology, a translation unit (or more casually a compilation unit) is the ultimate input to a C or C++ compiler from which an object file is generated. ISO/IEC 9899:TC3 - Committee Draft of the C99 Standard - Section 5.1.1.1 A translation unit roughly consists of a source file after it has been processed by the C preprocessor, meaning that listed in #include directives are literally Transclusion, sections of code within #ifndef may be included, and macros have been expanded. A C++ module also comprises a single translation unit, though while in a module #include directives will be expanded, import statements do not undergo expansion and thus are not.
Preprocessing notably consists of expanding a source file to recursively replace all #include directives with the literal file declared in the directive (usually , but possibly other source files); the result of this step is a preprocessing translation unit. Further steps include macro expansion of #define directives, and conditional compilation of #ifdef directives, among others; this 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.
Note that the preprocessor is in principle language agnostic, and is a lexical preprocessor, working at the lexical analysis level – it does not do parsing, and thus is unable to do any processing specific to C syntax. The input to the compiler is the translation unit, and thus it does not see any preprocessor directives, which have all been processed before compiling starts. While a given translation unit is fundamentally based on a file, the actual source code fed into the compiler may appear substantially different than the source file that the programmer views, particularly due to the recursive inclusion of headers.
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".
If a C++ module is split into module interface and module implementation, its translation unit consists of both the interface and implementation. A module translation unit also includes all its module partitions.
|
|