Should a header file includes another header file?

Submitted by Eus
on July 10, 2009 - 3:12am

Now that I'd got another chance to develop a C application, whenever I crafted a header file, I always wondered whether the header file needed to include another header file such as stdio.h when there was a function prototype in my header file that spelled `FILE *'. Googling a bit for "#include in header or source file", I found this interesting thread: http://www.velocityreviews.com/forums/t316283-header-include-order.html. Reading it, I can conclude the following points:

1. Althouth the order of header inclusions should not matter, it is a good idea to lay them as follows:
a. System headers
b. Application headers
c. The header associated with this source file
because if there is a redefinition of a variable or struct or the like in (b) or (c), the compiler will point out the error to be in (b) or (c) but not in (a). Although reversing the order of inclusion to be (c), (b), and then (a) will force (c) to include any needed header, for example stdio.h when a prototype in (c) spells `FILE *', any redefinition in (b) or (c) will cause the compiler to complain about (a) instead of (b) or (c).

2. As for forcing (c) to include any needed header, there is a suggestion to not include any header file in (c) if forward declaring the needed types suffices (i.e., instead of including stdio.h, just declare `extern FILE;'). Unfortunately, this won't work for common stuff like `FILE *' or `size_t', and thinking which part can be forward-declared is just too much hassle. So, I think it is a good idea to include any necessary header file that (c) needs in (c), and include the same headers again in the source code in the order specified in (1).

Yes.

Anonymous (not verified)
on
July 21, 2009 - 4:44am

Yes.

Otherwise your program using your own headerfile will have to include stdio.h , and then you get a include file dependency where the order of your include files matter, and it shouldn't be like that.

Yes

Anonymous (not verified)
on
July 22, 2009 - 2:41am

Simply yes. A header file should include all that it needs. For the simple reason that people should not need to 'find out' which particular haeaders need including _before_ your (library?) header.

ifdef protection

strcmp
on
July 26, 2009 - 2:03pm

most header files are protected with

#ifdef THIS_HEADER_WAS INCLUDED
#define THIS_HEADER_WAS_INCLUDED 1
...
#endif

to prevent against multiple inclusion. you can rely on that and include the headers where needed. this only costs some minimal amount of time in the build process, but it's worth it, if you untangle the include dependencies and get self-contained headers. you only have to be careful if you develop against a certain standard and need feature-test-macros like the ones listed in /usr/include/features.h; these alter the expansion of the system headers and you have to put the e.g.

#define _BSD_SOURCE 1

or

#define _LARGEFILE64_SOURCE 1

_before_ any system header gets included -- and because other headers may include system headers, you have to put it before every include.

Thanks

Eus
on
July 27, 2009 - 1:54am

Hi Ho!

Thanks for all the comments :-)

Best regards,
Eus (FSF member #4445)

In this digital era, where computing technology is pervasive,
your freedom depends on the software controlling those computing devices.

Join free software movement today!
It is free as in freedom, not as in free beer!

Join: http://www.fsf.org/jf?referrer=4445

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.