[vox-tech] Make question: headers depending on other headers

Mark K. Kim vox-tech@lists.lugod.org
Wed, 31 Mar 2004 15:22:30 -0800 (PST)


On Wed, 31 Mar 2004, Bill Kendrick wrote:

> Is it useful to list these 'dependencies' within the Makefile?

I do it, 'cuz I've seen it handled incorrectly at times.  But there are
some caveats... (see below)

> For example, consider foo.c:
>
>   /* foo.c
>      I do foo things */
>
>   #include "foo.h"  /* My header */
>   #include "bar.h"  /* bar.c's header, so I can get to its function(s) and/or
>                        typedefs... */
>
>   ...
>
>
> In Makefile, I'd do:
>
>   foo.o:  foo.c foo.h bar.h
>           ...compile foo.c into foo.o...
>
>
> However, say in "bar.h", I have:
>
>
>   /* bar.h
>      I do things with alcohol (get it? "bar?" hahaha...) */
>
>   #include "bar.h"  /* My header */
>   #include "zzz.h"  /* Contains some #define's for compile-time options */
>
>   ...
>
>
> Should I make "bar.h" depend on "zzz.h"?  Or "foo.o" depend on it?  Or...?

Since bar.h depends on zzz.h, you wanna do:

   bar.h: zzz.h
      touch bar.h

But if you Ctrl-C while `make` is running `touch bar.h`, make will erase
bar.h.  Make's reasoning is that bar.h depends on zzz.h, which probably
means bar.h can be recreated from zzz.h, and since you interrupted the
creation of bar.h mid-way (although you're really just touching it) bar.h
is probably only half-created, so it'll need to be fully-recreated later
when you run `make` again - so just remove the file so it doesn't mess up
dependencies later.

This is a fine rule for most cases, but not if you're just touching a file
simply because there is a depency.  So what I do this tell `make` not to
remove bar.h if I Ctrl-C:

   .PRECIOUS: bar.h

Or if you want all header files marked "precious":

   .PRECIOUS: %.h

which is fine unless you got a *.h file that does get recreated from
another file (usually okay.)

.PRECIOUS is supported by another make implementation I've used in the
past (besides GNU Make), so I'm guessing it's a pretty standard built-in
target name.

-Mark


-- 
Mark K. Kim
AIM: markus kimius
Homepage: http://www.cbreak.org/
Xanga: http://www.xanga.com/vindaci
Friendster: http://www.friendster.com/user.jsp?id=13046
PGP key fingerprint: 7324 BACA 53AD E504 A76E  5167 6822 94F0 F298 5DCE
PGP key available on the homepage