Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translation unit vs Compilation unit vs object file vs executable vs.... in C++

I couldn't find the difference between translation unit, compilation unit, object file, executable...At many places I've seen that one is used instead of other.

I am aware that these files are generated during C++ program compilation and linkage. Can anyone list out all the files that would be generated during these processes and specify the differences between them?

like image 205
pasha Avatar asked Dec 27 '15 10:12

pasha


1 Answers

Translation unit is the same as compilaition unit (so your source and all the header files it includes)

Object file, in typical cases, is the result of the compilation unit being compiled.

Executable file is the result of linking the object file(s) of a project, together with the runtime library function.

Exactly what files are actually generated during compilation depends on the compiler, but most modern compilers will simply read the source file and headers, then produce the object file, which is passed to the linker directly if you only have one source file. This produces the executable file.

Older compilers would "preprocess" as a separate step, so you'd end up with all the compile-unit in a temporary file. Similarly, in the old days, instead of generating machine code in the object file, assembler code would be output by the compiler, and then assembled through the an assembler, and this would make the object file. Linking remains similar.

Note that this is just practice, there is nothing in the C or C++ standards about executable files or object files. It's up to the compiler implementation to solve those things in whatever fashion they like.

like image 174
Mats Petersson Avatar answered Sep 28 '22 09:09

Mats Petersson