On Mon, 2023-10-02 at 12:00 -0700, Bahman Movaqar wrote:
> Thanks for the hint re style.  Finding good examples of writing easy-
> to-read make files is no easy feat.  That's why I do have these
> inaccuracies in my personal style.

I should say that the original method you posted is unusual IF YOU WANT
TO USE $<.  It's not an inherently "bad" style in general.

You should put the "important" prerequisite, if one exists, on the rule
containing the recipe.  If that rule doesn't list any prerequisites,
you are effectively saying that none of the prerequisites is special
and they are all interchangeable.  In that case, $< doesn't really make
sense; why would you need to get the first prerequisite specifically,
if all the prerequisites are interchangeable?

The canonical way to think about this is compiling C (or similar) code
into an object file.  Here, there is exactly one source file, then zero
or more extra prerequisites (header files).  The correct way to write
this rule is to put the source file in the rule that defines the
recipe, then you can use $<.  So either:

    foo.o: foo.c
            $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

    foo.o: foo.h bar.h baz.h biz.h

Or, using a pattern rule:

    %.o: %.c
            $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

    foo.o: foo.h bar.h baz.h biz.h

You wouldn't want to write:

    foo.o:
            $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

    foo.o: foo.c foo.h bar.h baz.h biz.h

because although the above formulation work, this one would fail badly:

    foo.o:
            $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

    foo.o: foo.h bar.h baz.h biz.h
    foo.o: foo.c

Reply via email to