320

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:

  1. What exactly it is?

  2. When should I consider using it while programming with C++?

  3. Is it related to C++ only, or it can be used with other programming languages as well?

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

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Harry
  • 3,592
  • 5
  • 21
  • 16
  • 1
    2. You are already using the translation unit if you have included header files. It is a term used for reference and not a c++ construct per say – talekeDskobeDa Sep 19 '19 at 15:57

11 Answers11

335

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.

JeffH
  • 10,254
  • 2
  • 27
  • 48
  • 23
    Is the term used only in C/C++? – dekuShrub Apr 18 '18 at 09:21
  • 6
    @dekuShrub as a matter of fact, no. For example, in Rust, a translation unit is a crate, in C++ same thing would be referred to as an entire library. The term itself is universal, but it definitely started with C. –  Dec 06 '19 at 18:02
  • 2
    New reference which states roughly what this answer states: https://en.wikipedia.org/wiki/Translation_unit_(programming) – Gabriel Staples May 11 '20 at 17:09
  • Is it possible to see the contents of a translation unit before it's compiled? – 425nesp Apr 13 '22 at 20:28
  • @425nesp GCC previously had the `-fdump-lang-raw` option, but [it was recent removed](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82542). Other compilers/tools have similar options: https://stackoverflow.com/q/39140779/159145 – Dai Oct 15 '22 at 03:44
89

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

http://web.archive.org/web/20091213073754/http://msdn.microsoft.com/en-us/library/bxss3ska(VS.80).aspx

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • 3
    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 Jul 09 '09 at 20:08
  • 16
    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 Jul 09 '09 at 21:11
  • @LightnessRacesinOrbit: Yeah, what I was trying to say was that it's unorthodox to directly compile a header as a TU, rather than indirectly compile it into a TU via inclusion. Deleted the first comment for being plain wrong, keeping the second to give our new ones context. – GManNickG Feb 04 '13 at 20:02
  • 1
    @GManNickG: How about ".h files are conventionally not fed directly to the compiler." – Lightness Races in Orbit Feb 05 '13 at 01:38
  • 2
    @JohannesSchaub-litb I think you mean link, not compile. You can compile any file as long as it is proper C/C++ with all names defined. It would be useless to compile a header file since the whole point of a header file is to be included (read copied) into source files, so they are already being compiled when you compile a source file that includes it. I guess what you meant to say is that you cannot create an executable from a file that does not have a main function. – pooya13 Mar 28 '19 at 02:32
  • So is it the object file? – Alec Mather Dec 04 '22 at 16:22
36

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:

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

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

Pharap
  • 3,826
  • 5
  • 37
  • 51
  • 1
    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. –  Jul 09 '09 at 21:10
  • 1
    @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 Jul 09 '09 at 21:19
  • 6
    @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 Jul 09 '09 at 21:43
7

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

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 1
    No. If he was talking about the source code he would say source files. The translation unit is made by compiling the source code. Note the distinct difference. It is "Translated" source code. – Dan Feb 22 '14 at 06:44
  • 4
    @Dan: No, it's not. A translation unit is a source file after includes which *can* be compiled, i.e., the output of the preprocessor prior to compilation. – Ed S. Feb 22 '14 at 06:47
  • the Preprocessor which just happens to be part of the compilation process. Did not mean to tweak your beak, but when you talk about a "Translation Unit" there is a distinct difference between that which has been preprocessed and that which has not. They are not interchangeable nouns. And thats my point. – Dan Feb 22 '14 at 08:37
  • 1
    In fact, despite what the C++ standard calls it, "translation unit" is commonly used to communicate the idea of a single "Unit" of compiled code. In fact according to the Microsoft compiler guys, you link "Translation Units" directly. http://msdn.microsoft.com/en-us/library/vstudio/bxss3ska(v=vs.100).aspx – Dan Feb 22 '14 at 08:49
  • 1
    So are we trying to be "C++ standard" nazis or are we trying to help people communicate with the rest of the industry? I know this is a C++ thread so I wont go in to what xcode calls a tu. Or all the other definitions of the term. – Dan Feb 22 '14 at 08:51
  • 4
    @Dan: A translation unit is what the standard calls it. Im not really concerned with the opinion of random compiler devs. Interesting that the guy who digs up a nearly five year old post to nitpick and tell me that my definition is wrong turns around and calls me a "language nazi" for correcting his. Yeesh, move on, you're tiring to deal with. – Ed S. Feb 22 '14 at 19:11
5

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
  • 28,661
  • 12
  • 68
  • 93
5

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

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
Allan Stokes
  • 515
  • 6
  • 5
3

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

1

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
  • 91
  • 5
0

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
  • 46,370
  • 69
  • 194
  • 251
0

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
  • 119,563
  • 19
  • 122
  • 198
  • 1
    "you would only need to worry about it if you were writing a C or C++ compiler." I disagree: programmers often need to understand what the compiler is doing. So, for instance, you need to know what a translation unit is in order to understand an important point from Item #5 in Effective C++: "the relative order of initialization of non-local static objects defined in different translation units is undefined". – Channing Moore Aug 06 '15 at 17:27
0

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

Yang
  • 1
  • 2