17

Is there a difference between what a translation unit is in C++ and C?

In other posts, I read that a header and source file makes a translation unit, but can a source file alone be called a translation unit in C++ where it contains all the definitions in one file?

user103214
  • 3,478
  • 6
  • 26
  • 37
  • 6
    Preprocessed source files are translation units. – Kerrek SB Dec 01 '11 at 13:34
  • 2
    @KerrekSB Isn't that an answer? – daramarak Dec 01 '11 at 13:37
  • 1
    No, just sometimes I think an "answer" should comprise a certain minimum of text, and when I don't have that much to say I just comment... – Kerrek SB Dec 01 '11 at 13:41
  • @KerrekSB, you have a point here. I think, though, that it's a matter of information conveyed in as short a text as possible. And your comment (again, in my opinion) perfectly answers the description. – Michael Krelin - hacker Dec 01 '11 at 13:46
  • 3
    @MichaelKrelin-hacker: It's actually true that some of the "Famous Answers" are one-liners... call it a personal reluctance; there's no sophisticated reason behind most things I do :-) – Kerrek SB Dec 01 '11 at 13:47
  • possible duplicate of [What is a "translation unit" in C++](http://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c) – Alok Save Dec 01 '11 at 15:27

5 Answers5

34

A translation unit is not "a header and a source file". It could include a thousand header files (and a thousand source files too).

A translation unit is simply what is commonly known as "a source file" or a ".cpp file" after being preprocessed. If the source file #includes other files the text of those files gets included in the translation unit by the preprocessor. There is no difference between C and C++ on this matter.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • If I put my class definition in `.cpp` instead of using a `.h` to put class declaration. Can I call that `.cpp` a translation unit? – user103214 Dec 01 '11 at 13:40
  • 1
    Yes, there's no requirement of having header files. – R. Martinho Fernandes Dec 01 '11 at 13:42
  • 3
    @user974191: you should use headers to avoid copying your code over and over. The `#include` mechanism allows you to write once and have the compiler copy it for you: it's a matter of code organization though, and has not much to do with the concept of translation unit. – Matthieu M. Dec 01 '11 at 13:55
  • It could include a thousand header files (and a thousand source files too).// I don't understand. How is it possible for a translation unit to have many .c files? Please help – Karthik Raj Palanichamy Sep 27 '17 at 10:13
  • 1
    @Karthik the same way you do with headers: `#include`. It's not a great idea, though. – R. Martinho Fernandes Sep 27 '17 at 13:07
3

Header is added to the .cpp file on preprocessing, so the compilator is basically working on a big chunk of code, containing both .cpp and all of .h added by "#include".

That's the translation unit.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
1

It depends on what you mean by “difference”. Both C and C++ define it similarly: basically, everything that gets compiled when you compile a source file (thus, all of the included headers, expanded macros, etc.). But that's not the same thing in the two languages; things like templates mean that translation units do behave differently in C++ than in C. (C++ has the one definition rule, for example.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Is it ok to have two class definitions with same name in two .cpp files by One definition rule? thanks. – user103214 Dec 01 '11 at 14:13
  • @user974191 Only if they consist of exactly the same tokens, and all of the symbols bind to exactly the same thing. This is why unnamed namespaces were introduced: put the local classes in an unnamed namespace, and their fully qualified name is different; they are no longer the same class, because they no longer have the same name. – James Kanze Dec 01 '11 at 15:17
0

A translation unit is actually what you get once the source and header files have passed through preprocessing (which expands the source using the header files) and precompilation. The compiler uses the translation unit to produce the .obj files that you see in your compiler output directory.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
ChrisBD
  • 9,104
  • 3
  • 22
  • 35
  • - precompilation (there is no such step as far as I know) – Matthieu M. Dec 01 '11 at 13:54
  • 1
    production of precompiled headers - fairly common on large C++ projects. – ChrisBD Dec 01 '11 at 15:22
  • Ah! I would not really dub it precompilation, but I now understand. As for being common... yes and no. I am working on projects that share millions of lines and the best bang for the buck was not to precompile headers, but rather to share the produced objects between developers of a same team (CCache) and distribute the build. – Matthieu M. Dec 01 '11 at 15:40
-2

set of source files seen by the compiler and translated as a unit