Pedantic: What Is A Source File? What Is A Header?

Viewed 12012

For the purposes of this question, I am interested only in Standard-Compliant C++, not C or C++0x, and not any implementation-specific details.

Questions arise from time to time regarding the difference between #include "" and #include <>. The argument typically boils down to two differences:

  1. Specific implementations often search different paths for the two forms. This is platform-specific, and not in the scope of this question.
  2. The Standard says #include <> is for "headers" whereas #include "" is for a "source file." Here is the relevant reference:

ISO/IEC 14882:2003(E)

16.2 Source file inclusion [cpp.include]

1 A #include directive shall identify a header or source file that can be processed by the implementation.

2 A preprocessing directive of the form

# include  < h-char-sequence > new-line
searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

3 A preprocessing directive of the form

# include "q-char-sequence" new-line
causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read
# include  < h-char-sequence > new-line
with the identical contained sequence (including > characters, if any) from the original directive.

(Emphasis in quote above is mine.) The implication of this difference seems to be that the Standard intends to differentiate between a 'header' and a 'source file', but nowhere does the document define either of these terms or the difference between them.

There are few other places where headers or source files are even mentioned. A few:

158) A header is not necessarily a source file, nor are the sequences delimited by in header names necessarily valid source file names (16.2).

Seems to imply a header may not reside in the filesystem, but it doesn't say that source files do, either.

2 Lexical conventions [lex]

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

This is the closest I could find to a definition, and it seems to imply that headers are not the "text of the program." But if you #include a header, doesn't it become part of the text of the program? This is a bit misleading.

So what is a header? What is a source file?

5 Answers
Related