views:

306

answers:

3

What's the diference between source file and translation unit?

+16  A: 

A "translation unit" is a source file plus any headers or other source files it #includes, plus any files that THEY include, and so on. A source file is just that...one source file.

If it helps any, think of the source file as the "before" the preprocessor runs, and the translation unit as "after".

cHao
+8  A: 

A translation unit is the basic unit of compilation in C++. It contains:

  • all the contents of a single source file after the preprocessor has run its course
  • the contents of any header files directly or indirectly included by it
  • minus any lines ignored using conditional preprocessing statements

A single translation unit gets compiled into an object file, library, or executable program.

A source file, by contrast, is a stand-alone file, just like any other file on your file system. Once compiled, it can be a component of a translation unit as mentioned above.

John Feminella
+17  A: 

From C++ Standard:

A source file toghether with all the headers and source files included via the preprocessing directive #include less any source line skipped by any of the conditional inclusion preprocessing directives is called a translation unit.

There is nothing we can do