Translation Unit in C++
A Translation unit in C++ is a file that contains C++ code that the C++ compiler will compile and generate an Object file for.
What is a Translation unit?
A translation unit is similar but different from a .c++
file. To better understand this, l will explain what a file is in C++.
A file is an object that is stored in persistent storage and loaded into memory when needed.
A .c++
file is a file that contains text that is known as C++ source code. Each .c++
file in a C++ project is referred to as a translation unit. This translation unit is what is converted to object files during compilation.
What is an Object File?
Object files are binary code generated by the compiler after compilation of C++ translation units.
Difference between C++ Files and Translation Units
There is a difference between a .c++
file and translation units in that a .c++
file can have other .c++
file inside of it and the .c++
file with other embedded .c++
file becomes a single translation unit even though there are multiple .c++
files involved. This is because an object file can only be generated for the single .c++
file, in this case, the .c++
file that houses the other .c++
file.
But if in a C++ project, there are multiple .c++
file that are created separately and not embedded in a .c++
file, then the number of translation unit equals the number of standalone .c++
file in the C++ project.
Summary
A Translation unit in C++ is a file that contains C++ code that the C++ compiler will compile and generate an Object file for.
A translation unit can be multiple C++ files, (in the case of having multiple C++ files in one C++ file), and multiple standalone C++ files are multiple translation units.
Basically, a translation unit in C++ is a file that contains C++ code that will be compiled to an Object file.
And this is a simplified breakdown of what a translation unit is in C++.
Other Resources
Here are other articles I have written about the concepts written in this article.
Hope you have learnt something from this article.
Thanks for reading. 🙂