As far as I know, a translation unit consists of a single implementation file .cpp/.c and all its included headers' code. When including a .cpp file inside another .cpp file, or including a .cpp file inside a .h file that is included inside yet another .cpp file, what is the translation units we have here?
Asked
Active
Viewed 102 times
1 Answers
1
When including a .cpp file inside another .cpp file, or including a .cpp file inside a .h file that is included inside yet another .cpp file, what is the translation units we have here?
A translation unit is a single file that is being compiled. Here it's the "outernmost" .cpp
file.
That said, #including
a .cpp
inside .h
is a really bad idea™️.

Employed Russian
- 199,314
- 34
- 295
- 362
-
inclusion of .cpp inside .h is just a question-based assumption to figure out how translation units are made. Back to a .cpp including another .cpp, if the outermost file is the translation unit source file, then which translation unit the included .cpp belong to if any. If non, then translation units can be less in number than source files, Am I right? – Physician Nov 22 '21 at 17:19
-
1@Physician A translation unit is whatever you feed to the compiler. If `outer.cpp` includes `inner.cpp` and you compile `outer.cpp`, then that's the translation unit. If you compile both `outer.cpp` and `inner.cpp` separately, then that's two TUs (linking them together will produce duplicate symbols, unless some symbol renaming is being done). – Employed Russian Nov 22 '21 at 17:45