dave@motto.UUCP (dave brown) (09/12/89)
I am writing a header file to be used by other programmers. Within this file, one of the structures needs the type size_t. This means that in order to compile properly, <stddef.h>, which defines size_t, must be included as well. Since my header file will be used in programs I didn't write, I need to figure out how to make sure <stddef.h> is included at the same time. Here are the ideas I have considered: 1. Document the requirement The documentation that tells the programmers to use <myheader.h> would also tell them it requires <stddef.h>. I would rather make things automatic, since if they neglect to include <stddef.h>, the result will be a compile error inside my file, which is a rather messy way of telling them they left something out. 2. Include <stddef.h> at the top of my header What if they have already included it? Is it guaranteed that it can be included more than once without causing errors? Are there any other ideas? What should I do? Thanks for your help, Dave ----------------------------------------------------------------------------- | David C. Brown | uunet!mnetor!motto!dave | | Motorola Canada, Ltd. | 416-499-1441 ext 3708 | | Communications Division | Disclaimer: Motorola is a very big company | -----------------------------------------------------------------------------
datanguay@watmath.waterloo.edu (David Adrien Tanguay) (09/13/89)
In article <71@motto.UUCP> dave@motto.UUCP (dave brown) writes: >I am writing a header file to be used by other programmers. Within >this file, one of the structures needs the type size_t. This means >that in order to compile properly, <stddef.h>, which defines size_t, >must be included as well. Since my header file will be used in >programs I didn't write, I need to figure out how to make sure ><stddef.h> is included at the same time. See section 4.1.2: the implementation must make the standard headers idempotent (with the exception of assert.h, which relies on NDEBUG). You can put <stddef.h> in your header file, and there should be no problems if the user of your header file also includes <stddef.h>. "...; each may be included more than once in a given scope, with no effect different from being included only once, ..." David Tanguay
gwyn@smoke.BRL.MIL (Doug Gwyn) (09/13/89)
In article <71@motto.UUCP> dave@motto.UUCP (dave brown) writes: >2. Include <stddef.h> at the top of my header Recommended, since <stddef.h> is an X3J11 invention and therefore you can expect it to follow the official rules for system headers... > What if they have already included it? Is it guaranteed that it can > be included more than once without causing errors? ...which includes the requirement that the system header be includable multiple times with no effect different from being included just once.
sabbagh@acf5.NYU.EDU (sabbagh) (09/13/89)
In article <71@motto.UUCP> dave@motto.UUCP (dave brown) writes: >I am writing a header file to be used by other programmers. Within >this file, one of the structures needs the type size_t. This means >that in order to compile properly, <stddef.h>, which defines size_t, >must be included as well. Since my header file will be used in >programs I didn't write, I need to figure out how to make sure ><stddef.h> is included at the same time. > >Here are the ideas I have considered: > >1. Document the requirement > [stuff deleted] >2. Include <stddef.h> at the top of my header As far as I have seen, 1. is the usual approach. If you want to have <stddef.h> included in your own header file (as per 2.) the best way to assure that it works is 1. Scan stddef.h for some symbol defined with an #define, say, #define FOO 2. In your header, use the following: #ifndef FOO #include <stddef.h> #endif This will guarantee that your header file will always have the stddef.h loaded, but it will cause problems for the user if they try to #include <stddef.h> AFTER #including your header. A third approach (if your are REALLY serious), is to copy those parts of <stddef.h> into your own header file and #undef them when your are done. This resolves all the above problems, but gives up something in portability. Isn't C great? Hadil G. Sabbagh E-mail: sabbagh@csd27.nyu.edu Voice: (212) 998-3285 Snail: Courant Institute of Math. Sci. 251 Mercer St. New York,NY 10012 186,282 miles per second -- it's not just a good idea, it's the law!
seanf@sco.COM (Sean Fagan) (09/16/89)
In article <71@motto.UUCP> dave@motto.UUCP (dave brown) writes: >Since my header file will be used in >programs I didn't write, I need to figure out how to make sure ><stddef.h> is included at the same time. Since the dpANS requires that the header files be multiply-includable, why not just include it in your header file? It's a nice, simple solution... -- Sean Eric Fagan | "Time has little to do with infinity and jelly donuts." seanf@sco.COM | -- Thomas Magnum (Tom Selleck), _Magnum, P.I._ (408) 458-1422 | Any opinions expressed are my own, not my employers'.