Translation unit vs Compilation unit vs object file vs executable vs.... in C++
Asked Answered
K

1

17

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?

Kame answered 27/12, 2015 at 10:36 Comment(0)
E
26

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.

Eyla answered 27/12, 2015 at 10:46 Comment(2)
one question: If the (static)linker links object files together with the runtime library functions, then how can it be a "runtime" library? Shouldn't it be a static library, if it can be linked during the static linking phase? I thought the runtime libraries are dynamically linked (usually by the loader of the OS) after the executable(binary) file is made by the static linker. Could you please elaborate on this?Exocrine
@starriet (to myself..) "runtime library" can be both dynamically OR statically linked, you can set it with compiler options. See this answer and this MSDN on MSVC compiler options.Exocrine

© 2022 - 2024 — McMap. All rights reserved.