Is a header file a translation unit?

671 Views Asked by At

Is a header file a translation unit? If I add the static keyword to a variable in a header file, could I call that variable in my .c or .cpp file? Thanks.

2

There are 2 best solutions below

0
alinsoar On

No, a translation unit is the full output of the preprocessor. A header file will be included and its content may become a part of the translation unit.

0
HolyBlackCat On

No, headers are not separate translation units.

Each .c/.cpp file is a separate translation unit, and since preprocessor textually replaces #includes with the contents of the headers, code from all headers included (directly or indirectly) by a .c/.cpp file is a part of that file's translation unit.

In other words, a translation unit is a .c or .cpp file after preprocessing.