| Start |
|---|
| Introduction There are a number of guidelines to follow when creating and using header files in a C project: If a header file is included multiple times in a translation unit (TU), it should not break builds. If you need the facilities declared in a header file, you should not have to include any other headers explicitly. You should not be able to remove any information from a header without causing builds to fail. Of more concern to C++ than C, but nevertheless important in C too. If the code in a TU (call it `code.c`) directly uses the features declared by a header (call it `"headerA.h"`), then `code.c` should `#include "headerA.h"` directly, even if the TU includes another header (call it `"headerB.h"`) that happens, at the moment, to include `"headerA.h"`. Occasionally, there might be good enough reasons to break one or more of these guidelines, but you should both be aware that you are breaking the rule and be aware of the consequences of doing so before you break it. |
| Self-containment Modern headers should be self-contained, which means that a program that needs to use the facilities defined by Recommendation: Header files should be self-contained.Historical rulesHistorically, this has been a mildly contentious subject. Once upon another millennium, the AT&T Indian Hill C Style and Coding Standards stated: Header files should not be nested. The prologue for a header file should, therefore, describe what other headers need to be `#include`d for the header to be functional. In extreme cases, where a large number of header files are to be included in several different source files, it is acceptable to put all common `#include`s in one include file. This is the antithesis of self-containment. Modern rulesHowever, since then, opinion has tended in the opposite direction.
If a source file needs to use the facilities declared by a header
and (subject only to having the correct search paths set on the command
line), any necessary pre-requisite headers will be included by
This provides better modularity for the source code. It also protects the source from the "guess why this header was added" conundrum that arises after the code has been modified and hacked for a decade or two. The NASA Goddard Space Flight Center (GSFC) coding standards for C is one of the more modern standards — but is now a little hard to track down. It states that headers should be self-contained. It also provides a simple way to ensure that headers are self-contained: the implementation file for the header should include the header as the first header. If it is not self-contained, that code will not compile. The rationale given by GSFC includes: §2.1.1 Header include rationale This standard requires a unit’s header to contain `#include` statements for all other headers required by the unit header. Placing `#include` for the unit header first in the unit body allows the compiler to verify that the header contains all required `#include` statements. An alternate design, not permitted by this standard, allows no `#include` statements in headers; all `#includes` are done in the body files. Unit header files then must contain #ifdef statements that check that the required headers are included in the proper order. One advantage of the alternate design is that the `#include` list in the body file is exactly the dependency list needed in a makefile, and this list is checked by the compiler. With the standard design, a tool must be used to generate the dependency list. However, all of the branch recommended development environments provide such a tool. A major disadvantage of the alternate design is that if a unit’s required header list changes, each file that uses that unit must be edited to update the `#include` statement list. Also, the required header list for a compiler library unit may be different on different targets. Another disadvantage of the alternate design is that compiler library header files, and other third party files, must be modified to add the required `#ifdef` statements. Thus, self-containment means that: Checking self-containmentSee Linking against a static
library for a script
|
| Minimality Headers are a crucial consistency checking mechanism, but they should be as small as possible. In particular, that means that a header should not include other headers just because the implementation file will need the other headers. A header should contain only those headers necessary for a consumer of the services described. For example, a project header should not include If the headers are minimal, then it keeps the compilation time to a minimum too. It is possible to devise headers whose sole purpose is to include a lot
of other headers.
These seldom turn out to be a good idea in the long run because few
source files will actually need all the facilities described by all the
headers.
For example, a |
| Notation and Miscellany The C standard says that there is very little difference between the
[`#include [`#include "header.h"`] 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 So, the double quoted form may look in more places than the angle-bracketed form. The standard specifies by example that the standard headers should be included in angle-brackets, even though the compilation works if you use double quotes instead. Similarly, standards such as POSIX use the angle-bracketed format — and you should too. Reserve double-quoted headers for headers defined by the project. For externally-defined headers (including headers from other projects your project relies on), the angle-bracket notation is most appropriate. Note that there should be a space between A number of projects use a notation such as: You should consider whether to use that namespace control in your
project (it is quite probably a good idea).
You should steer clear of the names used by existing projects (in
particular, both If you use this, your code should be careful and consistent in the use of the notation. Do not use Header files should seldom if ever define variables. Although you will keep global variables to a minimum, if you need a global variable, you will declare it in a header, and define it in one suitable source file, and that source file will include the header to cross-check the declaration and definition, and all source files that use the variable will use the header to declare it. Corollary: you will not declare global variables in a source file — a source file will only contain definitions. Header files should seldom declare
Cross-references
|
| Idempotence If a particular header file is included more than once in a translation unit (TU),
there should not be any compilation problems.
This is termed 'idempotence'; your headers should be idempotent.
Think how difficult life would be if you had to ensure that There are two ways to achieve idempotence: header guards and the Header guardsHeader guards are simple and reliable and conform to the C standard. The first non-comment lines in a header file should be of the form: The last non-comment line should be All the operational code, including other Each name must be unique. Often, a name scheme such as One option would be to use a generated MD5 (or other) hash for the header guard name. You should avoid emulating the schemes used by system headers which frequently use names reserved to the implementation — names starting with an underscore followed by either another underscore or an upper-case letter. The |
| Include What You Use (IWYU) Google's Include What You Use (opens new window) project, or IWYU, ensures source files include all headers used in the code. Suppose a source file The IWYU philosophy maximizes the probability that code continues to compile even with reasonable changes made to interfaces. Clearly, if your code calls a function that is subsequently removed from the published interface, no amount of preparation can prevent changes becoming necessary. This is why changes to APIs are avoided when possible, and why there are deprecation cycles over multiple releases, etc. This is a particular problem in C++ because standard headers are allowed to include each other. Source file IWYU would spot the problem and recommend that |
Read Article
https://develop.horje.com/learn/1434/reference