Translation Unit
Let's use a simple example of four files: A.h
, B.h
, A.cpp
, B.cpp
.
* A.cpp
includes A.h
.
* A.h
includes B.h
.
* A.cpp
contains the main()
function.
* B.cpp
includes B.h
.
To build the files you may use a single command:
g++ A.cpp B.cpp -o my_program
Or you can also make it more clear:
g++ -c A.cpp -o A.o
g++ -c B.cpp -o B.o
g++ A.o B.o -o my_program
The three stages of building: * Preprocessing. * Compilation. * Linking.
The preprocessing is to produce the translation units. So, in our example, we will produce 2 translation units. One for A and one for B.
Preprocessing is the a process of fetching all the included files (*.h
)
recursively and paste them into one giant file. This expanded code forms
the translation unit.
So, A.cpp
would fetch A.h
, then from A.h
to B.h
.
B.cpp
would fetch B.h
.
However, the "translation unit" only knows some of the function declarations,
but not definitions. If a function is declared in B.h
and is defined in
B.cpp
, the translation unit of A.cpp
would not have the definition of that
function.
It will wait till the linking to link the declaration to the definition.
So, the preprocessing is to collect all the entities involved without their definition (except for structs and classes). It allows the entities to refer to each other and create circular dependencies without causing an error.
The translation unit will later be compiled into .o
files. So, the
translation units have one-to-one mapping to the .o
files.