Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a header necessarily a file?

T.C. left an interesting comment to my answer on this question:

Why aren't include guards in c++ the default?

T.C. states:

There's "header" and there's "source file". "header"s don't need to be actual files.

What does this mean?

Perusing the standard, I see plenty of references to both "header files" and "headers". However, regarding #include, I noticed that the standard seems to make reference to "headers" and "source files". (C++11, § 16.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.

and

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.

I don't know if this is significant. It could be that "headers" in a C++ context unambiguously means "header files" but the word "sources" would be ambiguous so "headers" is a shorthand but "sources" is not. Or it could be that a C++ compiler is allowed leeway for bracket includes and only needs to act as if textual replacement takes place.

So when are header (files) not files?

The footnote mentioned by T.C. in the comments below is quite direct:

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

like image 560
Praxeolitic Avatar asked Dec 03 '14 00:12

Praxeolitic


People also ask

Do all files have a header?

Headers are usually files, but they are not necessarily files. It is permissible for a compiler to predefine the contents of a header internaly.

Are header files necessary?

Yes, because it's still based on C. You can answer your own question: Don't use them and try to compile without them. If you can't, then the compilers still require them.

What type of file is a header file?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files.

Which one is not a header file?

Explanation: The iomanip header file is used to correct the precision of the values. 7. Which of the following header file does not exist? Explanation: There is no such header file <sstring> in C++.


2 Answers

For the standard header "files" the C++ standard doesn't really make a mandate that the compiler uses a file or that the file, if it uses one, actually looks like a C++ file. Instead, the standard header files are specified to make a certain set of declarations and definitions available to the C++ program.

An alternative implementation to a file could be a readily packaged set of declarations represented in the compiler as data structure which is made available when using the corresponding #include-directive. I'm not aware of any compiler which does exactly that but clang started to implement a module system which makes the headers available from some already processed format.

like image 191
Dietmar Kühl Avatar answered Oct 22 '22 06:10

Dietmar Kühl


They do not have to be files, since the C and C++ preprocessor are nearly identical it is reasonable to look into the C99 rationale for some clarity on this. If we look at the Rationale for International Standard—Programming Languages—C it says in section 7.1.2 Standard headers says (emphasis mine):

In many implementations the names of headers are the names of files in special directories. This implementation technique is not required, however: the Standard makes no assumptions about the form that a file name may take on any system. Headers may thus have a special status if an implementation so chooses. Standard headers may even be built into a translator, provided that their contents do not become “known” until after they are explicitly included. One purpose of permitting these header “files” to be “built in” to the translator is to allow an implementation of the C language as an interpreter in a free-standing environment where the only “file” support may be a network interface.

like image 44
Shafik Yaghmour Avatar answered Oct 22 '22 08:10

Shafik Yaghmour