Menu

(Please Forgive 1 More) Question on *.h Files

2008-08-16
2012-09-26
  • Chris Hammond

    Chris Hammond - 2008-08-16

    Thank you for your patience and for being so helpful. I have one more question about syntax. All of the header files in this book have the form

    ifndef RANDOM1_H

    define RANDOM1_H

    <Body>

    endif

    where the header file is named Random1.h. As I understand it, such a statement will include the Body if RANDOM1_H is included. But such an expression never occurs anywhere. Does the preprocessor somehow recognize RANDOM1_H as Random1.h? What is this doing?

     
    • cpns

      cpns - 2008-08-16

      It is a mechanism to prevent multiple definition caused by multiple inclusion. The name only has to be unique within the project, it could be anything, but convention and common sense dictates that it should be based on the file name.

      Notice the second line; That is where RANDOM1_H is defined. The first line tests to see if it is not defined. So you see the pre-processor does not need to recognize anything, it is all self contained - no magic.

      So the first time the file is included, the macro will not be defined, so the body is parsed - defining the macro at the same time. The second time the file is included, the macro is defined so the body is skipped, thus preventing multiple declarations.

      It is important to do this because in a project of any significant size there are likely to be complex dependencies and nested include files. So for example if file1.cpp needs declarations in x.h and y.h it should include them both, but y.h may itself include x.h, so while file1.cpp might just include y.h, you would not necessarily know that, neither should you have to, and that dependency may even get removed under maintenance, breaking file1.cpp. However if you did not have the 'include guards', including x.h and y.h would in fact cause multiple declarations because x.h would be included twice.

      The name of this macro only has to be unique, it can be called anything, there is no magic or significance. It is merely convention, and common sense that suggests that the macro is related to the file name - since that generally ensures uniqueness (although not necessarily, it is possible to break it). Personally I use the form XXXX_INCLUDE, so RANDOM1_INCLUDE in this case. I also use the clearer form of preprocessor syntax:

      if !defined RANDOM1_INCLUDE

      this form was introduced when C was standardised in 1989, but I think I must have been one of the few to actually have started using it habitually, because you don't seem to see it that often.

      Perhaps http://en.wikipedia.org/wiki/Include_guard provides a better explanation than me.

      Clifford.

       
    • Chris Hammond

      Chris Hammond - 2008-08-17

      That makes sense. Thanks.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.