Search code examples
compilationtranslation-unit

Translation unit when including a source file?


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?


Solution

  • 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™️.