This is a language idiom (I will comment it):
#ifndef HEADERFILE_H
Between this, and the last #endif
everything is included in compilation, but only if HEADERFILE_H
has not been defined before.
#define HEADERFILE_H
The first thing we do in the block is to #define
the identifier, so next time we find this fragment again later, the contents between #ifndef
and #endif
will not be #include
d again (because of the identifier declaration).
// some declarations in
// the header file.
this block will be included only once, even if you #include
this file several times.
#endif
and this marks the end of the protected block.
It is common to include some file that, indeed, #include
s another, and that file includes another, leading to a point in which you don't know which files have been included and which don't. This phrasing allows you to be protected, and to be able to #include
the same file several times (normally you cannot, as some definitions cannot be repeated in the same compilation unit, e.g. declarations) the lines above will include the contents and define the identifier, making next inclussions (that are effectively done) not to include the contents, as the identifier appears as #definen
in second and ulterior times.