Reputation: 3822
I am reading at the time the "Effective C++" written by Scott Meyers and came across the term "translation unit".
Could somebody please give me an explanation of the following:
What exactly is it?
When should I consider using it while programming with C++?
Is it C++ only, or can it be used with other programming languages?
I might already use it without knowing the term...
Upvotes: 343
Views: 119949
Reputation: 74654
A translation unit is for all intents and purposes a file (.c/.cpp), after it's finished including all of the header files.
Upvotes: 95
Reputation: 535
In addition to the ODR, the translation unit is important in the definition of unnamed namespaces, which replaces one of the old uses of "static".
Upvotes: 7
Reputation:
A hard question to answer definitively. The C++ standard states:
The text of the program is kept in units called source files in this International Standard. A source file together with all the headers (17.4.1.2) and source files included (16.2) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (16.1) preprocessing directives, is called a translation unit. [Note: a C++ program need not all be translated at the same time. ]
So for most intents and purposes a translation unit is a single C++ source file and the header or other files it includes via the preprocessor #include mechanism.
Regarding your other questions:
- When should I consider using it when programming with C++
You can't not use it - translation units are the basis of a C++ program.
- If it is related only to C++, or it can be used with other programming languages
Other languages have similar concepts, but their semantics will be subtly different. Most other languages don't use a preprocessor, for example.
Upvotes: 37
Reputation: 10482
From here: (wayback machine link)
According to standard C++ (wayback machine link) : A translation unit is the basic unit of compilation in C++. It consists of the contents of a single source file, plus the contents of any header files directly or indirectly included by it, minus those lines that were ignored using conditional preprocessing statements.
A single translation unit can be compiled into an object file, library, or executable program.
The notion of a translation unit is most often mentioned in the contexts of the One Definition Rule, and templates.
Upvotes: 353
Reputation: 31
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 not including sections of code removed by conditional-compilation directives such as #if, is called a "translation unit."
Upvotes: 3
Reputation: 1
In my view, a "translation unit" is typically a single "post-preprocessing" source file. You can get more details on this MSDN page. http://msdn.microsoft.com/en-us/library/bxss3ska(v=vs.80).aspx
Upvotes: 0
Reputation: 91
According to MSDN: 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 not including sections of code removed by conditional-compilation directives such as #if, is called a "translation unit."
Upvotes: 1
Reputation: 123468
As others have said, a translation unit is basically the contents of a source file after preprocessing. It's the topmost production in the language grammar; you would only need to worry about it if you were writing a C or C++ compiler.
Upvotes: 0
Reputation: 47900
Every cpp/c (implementation) file will be converted into a translation unit (ie.,object file (.obj)) headers in the cpp file will be replaced with the actual text from the header files.
Upvotes: 0
Reputation: 29759
A translation unit is code that is passed to the compiler proper. This typically means the output from running the preprocessor on the .c file.
Upvotes: 6
Reputation: 124642
The book makes it clear enough. When Meyers referes to a "translation Unit", he means a source code file.
Upvotes: 8