322

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

1
  • 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 Sep 19, 2019 at 15:57

11 Answers 11

336

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.

5
  • 23
    Is the term used only in C/C++?
    – dekuShrub
    Apr 18, 2018 at 9: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.
    – user11877195
    Dec 6, 2019 at 18:02
  • 2
    New reference which states roughly what this answer states: en.wikipedia.org/wiki/Translation_unit_(programming) May 11, 2020 at 17:09
  • Is it possible to see the contents of a translation unit before it's compiled?
    – 425nesp
    Apr 13, 2022 at 20:28
  • @425nesp GCC previously had the -fdump-lang-raw option, but it was recent removed. Other compilers/tools have similar options: stackoverflow.com/q/39140779/159145
    – Dai
    Oct 15, 2022 at 3: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

6
  • 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. Jul 9, 2009 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. Jul 9, 2009 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 4, 2013 at 20:02
  • 1
    @GManNickG: How about ".h files are conventionally not fed directly to the compiler." Feb 5, 2013 at 1: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, 2019 at 2:32
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.

3
  • 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.
    – anon
    Jul 9, 2009 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 9, 2009 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 :) Jul 9, 2009 at 21:43
7

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

6
  • 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, 2014 at 6: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, 2014 at 6:47
  • 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. msdn.microsoft.com/en-us/library/vstudio/…
    – Dan
    Feb 22, 2014 at 8: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, 2014 at 8: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, 2014 at 19:11
6

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

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

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

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.

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.

1
  • 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". Aug 6, 2015 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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.