0

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.

Serket
  • 3,785
  • 3
  • 14
  • 45
  • 1
    I voted to close as too broad since you have asked 2 questions. For your titular one, this would be a duplicate of [What is a “translation unit” in C++](https://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c) – underscore_d Jun 05 '20 at 10:30

2 Answers2

6

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.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
3

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.

alinsoar
  • 15,386
  • 4
  • 57
  • 74