Page 124 - C-Language
P. 124

Chapter 16: Create and include header files




        Introduction



        In modern C, header files are crucial tools that must be designed and used correctly. They allow
        the compiler to cross-check independently compiled parts of a program.

        Headers declare types, functions, macros etc that are needed by the consumers of a set of
        facilities. All the code that uses any of those facilities includes the header. All the code that defines
        those facilities includes the header. This allows the compiler to check that the uses and definitions
        match.



        Examples


        Introduction


        There are a number of guidelines to follow when creating and using header files in a C project:


            •  Idemopotence

              If a header file is included multiple times in a translation unit (TU), it should not break builds.


            •  Self-containment

              If you need the facilities declared in a header file, you should not have to include any other
              headers explicitly.


            •  Minimality

              You should not be able to remove any information from a header without causing builds to
              fail.


            •  Include What You Use (IWYU)

              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.


        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.


        https://riptutorial.com/                                                                             100
   119   120   121   122   123   124   125   126   127   128   129