tags:

views:

1654

answers:

8

Hello everybody I am reading at the time the "Effective C++" written by Meyers and came across the term "translation unit".

Could somebody please give me an explanation of:

1) What exactly it is

2) When should I consider using it when programming with C++

3) If it is related only to C++, or it can be used with other programming languages

I might already use it without knowing the term....

+8  A: 

A translation unit is for all intents and purposes a file (.c/.cpp), after it's finished including all of the header files.

http://msdn.microsoft.com/en-us/library/bxss3ska%28VS.80%29.aspx

Paul Betts
Got me by 24 seconds :-)
Ed Swangren
Not .h files. Header files are not compiled.
GMan
Including header files. Header files are processed by the compiler, even if no code is generated. See also JeffH's preprocessor comment, the definition "everything the compiler sees" is a good one.
Marco van de Voort
Header files are included into .cpp files, but .h themselves are not compiled.
GMan
You can compile files ending in ".h" just fine. The file-name isn't important at all. The content is. If the content of "foo.h" is "int main() { }" you can compile it.
Johannes Schaub - litb
+4  A: 

The book makes it clear enough. When Meyers referes to a "translation Unit", he means a source code file.

Ed Swangren
yes u r right :) .... i missed that paragraph.... thanks everybody..
Harry
+26  A: 

From here:

According to standard C++: 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.

JeffH
A great point! I edited my quote to include the hotlink on the "here" page. That link leads to a wiki page about ISO/IEC 14882 and in the External Links section of that page, there is a link to ISO/IEC 14882 on iso.org
JeffH
Whoops! The comment that "A great point!" refers to was deleted.
JeffH
Probably all the comments here, including this one, should be deleted :-)
anon
@Neil, yes i think that's a good idea :D
Johannes Schaub - litb
A: 

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.

sigjuice
A: 

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.

yesraaj
+4  A: 

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 direc- tives, 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:

2) When should I consider using it when programming with C++

You can't not consider it - translation units are the basis of a C++ program.

3) 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.

anon
So to clarify, headers are not compiled but their contents can be included into a translation unit upon which it will effectively be compiled.
GMan
I don't know if that clarifies or not. This can be a somewhat murky area - it's not clear from the standard para I quoted from that pre-compiled headers are allowed, for example.
anon
@GMan, and that's where you have to be very careful about the one definition rule. If you include a class in different translation units with slightly different defines before that class is included that cause the class to have different code it will cause undefined problems.
Matt Price
@GMan note the two terms used by the Standard: "header" and "source file". "header" is only used for the Standard library. A user file that's included by some code is not called "header" by the Standard, but "source file". The Standard doesn't know about the difference between ".h" and ".cpp" that we poor c++ programmers made up :)
Johannes Schaub - litb
A: 

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.

John Bode
A: 

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."

rahul