Is there a difference between what a translation unit is in C++ and C?
In other posts, I read that a header and source file makes a translation unit, but can a source file alone be called a translation unit in C++ where it contains all the definitions in one file?
A translation unit is not "a header and a source file". It could include a thousand header files (and a thousand source files too).
A translation unit is simply what is commonly known as "a source file" or a ".cpp file" after being preprocessed. If the source file #include
s other files the text of those files gets included in the translation unit by the preprocessor. There is no difference between C and C++ on this matter.
Header is added to the .cpp file on preprocessing, so the compilator is basically working on a big chunk of code, containing both .cpp and all of .h added by "#include".
That's the translation unit.
It depends on what you mean by “difference”. Both C and C++ define it similarly: basically, everything that gets compiled when you compile a source file (thus, all of the included headers, expanded macros, etc.). But that's not the same thing in the two languages; things like templates mean that translation units do behave differently in C++ than in C. (C++ has the one definition rule, for example.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With