MJTech
MJTech

Reputation: 121

Is a Translation Unit always a file?

In C++, when I ask someone what a translation unit is, the popular response is that it is usually a source file. static and extern modify how global functions and variables are accessed by other translation units. Can translation units be something else, too? For example, an assembly file (not .cpp with inline ASM)? Or a .dll? I've seen experienced software engineers make such programs.

My experience with C++ is largely with microcontrollers, so I am used to making a main.cpp, some .h files, and including a few libraries from PIO or GitHub, but not using ASM.

Another source of confusion, some say that all of the includes are also part of the same translation unit. If that were so, then static and extern would not behave the way I believe they do now. I would really like to clear some of the confusion associated with translation units.

Upvotes: 2

Views: 546

Answers (2)

Jesper Juhl
Jesper Juhl

Reputation: 31488

Is a Translation Unit always a file?

No, it does not have to be. For example, the standard library headers (and other source files) could in theory not be files in the filesystem. They could be something internal to the compiler, contained within the compiler executable, or found in a database, or whatever. A translation unit is usually a source file, but you cannot assume that.

Another source of confusion, some say that all of the includes are also part of the same translation unit

Sure they are. When a source file is compiled, the preprocessor has already run and basically copy+paste'd all the includes into the source file that the compiler sees. So, from the compiler's perspective, the includes are part of the compilation unit, since all their contents were pasted into the file the compiler was given. The compiler never sees #include directives - those are all handled by the preprocessor before the compiler ever sees the file/translation unit.

See also:

https://en.cppreference.com/w/cpp/language/definition

https://en.cppreference.com/w/cpp/keyword/static

https://en.cppreference.com/w/cpp/keyword/extern

Upvotes: 4

Yksisarvinen
Yksisarvinen

Reputation: 22394

From C++20 standard draft:

The text of the program is kept in units called source files in this document. A source file together with all the headers (16.5.1.2) and source files included (15.3) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (15.2) preprocessing directives, is called a translation unit. [Note: A C++ program need not all be translated at the same time. — end note]

So in general, translation unit is one C++ file together with all of its #included headers passed as input to C++ compiler. This is then translated (compiled). After translating all translation units, one can link them together to form a program.


Assembly file is not C++ and not considered by C++ standard. If a toolchain would link that to C++ program, it's that toolchain extension.

DLL (and other libraries) is a set of one or more already translated translation units. It can be linked with other translated translation units into a program.

Upvotes: 6

Related Questions