[vox-tech] C header files question

Jeff Newmiller vox-tech@lists.lugod.org
Wed, 17 Jul 2002 23:34:46 -0700 (PDT)


On Wed, 17 Jul 2002, Peter Jay Salzman wrote:

> during the course of writing a C program, often i'll change an
> implementation of the way i do things.
> 
> functions get added and deleted, but often i'll forget what header file
> was added for a particular function.
> 
> i know there are header files which aren't needed anymore.
> 
> is there a utility that lets you know which header files aren't needed
> for a particular file of C code?

Sounds like something a "lint" or compiler might do, but lclint doesn't
appear to do this.

I try to keep the total number of includes in any given file to a minimum.
Commenting a header you suspect of being unnecessary and attempting to
recompile is an effective solution for small numbers of headers.

One way to minimize headers is to define your own "modules":

+-myio.h------------+
| int do_input();   |
| void do_output(); |
+-------------------+

+-myio.c-------------------------------------+ +-mytoplevel.c------+
} #include "myio.h"                          | | #include "myio.h" |
| #include <stdio.h> /* scanf(), printf() */ | | int main() {      |
| int do_input() { ... }                     | |  do_input();      |
| void do_output() { ... }                   | |  ...              |
+--------------------------------------------+ |  do_output();     |
                                               +-------------------+

stdio.h here is representative of some large, complicated headers
associated with a powerful library.

While this example may seem trivial, in a large program the fact that only
myio.c depends on some huge external library can have far reaching
effects.  One effect is shortened compilation for all the modules that
depend on myio.h but don't directly include stdio.h.  This also localizes
the dependency, so if you decide to use <gtk-1.2/gdk/gdk.h> instead of
<stdio.h>, you don't even have to recompile any of the rest of your object
modules.  From a complexity control perspective, this is a good thing. :)

If you have more than 5 or 10 includes, you should probably ask yourself
if you are doing too much in one file.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil@dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...2k
---------------------------------------------------------------------------