[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bug or correct behavior?

From: Paul Smith
Subject: Re: Bug or correct behavior?
Date: Tue, 05 Feb 2008 14:40:11 -0500

On Tue, 2008-02-05 at 11:32 -0800, Derek Clegg wrote:
> Given this simple makefile
> start: dir/a.c
> dir/%.c: %.c | dir ; cp $< $@
> dir/%.c: foo.h
> dir: ; mkdir $@
> a.c foo.h: ; touch $@
> I'm seeing the following when I run make:
> touch a.c
> mkdir dir
> cp a.c dir/a.c
> This is surprising; my expectation is that the dependency "dir/%c.:  
> foo.h" would force "foo.h" to be created as well.  Is this a bug or  
> correct behavior?

This is correct.

Pattern rules are not the same as explicit rules: only one pattern rule
will ever match a given target.  Make goes through them in the order you
define them and whichever one matches first will be used; the rest will
be ignored.

This is necessary, because quite often you have multiple pattern rules
that build the same target but have different prerequisites.  As a
trivial example consider the builtin pattern rules make knows about:

        %.o : %.c
        %.o : %.f
        %.o : %.cpp

etc. etc.  There are so many ways to build a .o file!  You certainly
wouldn't want to be required to have all of foo.c, foo.f, foo.cpp, etc.
just to build a foo.o.

Anyway, I don't think you really want the .c file to depend on the .h
file, do you?  That's pretty unusual.  You don't need to copy the .c
file again just because the .h file changes?!?!

Instead I think you want the .o to depend on foo.h.

You can either add foo.h to the pattern, or you can declare it elsewhere
as an explicit prerequisite rather than a pattern, like:

        dir/a.o : foo.h

 Paul D. Smith <address@hidden>       
 "Please remain calm--I may be mad, but I am a professional."--Mad Scientist

reply via email to

[Prev in Thread] Current Thread [Next in Thread]