help-make
[Top][All Lists]
Advanced

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

Re: handling two types of prerequisites differently in pattern rule


From: Todd Showalter
Subject: Re: handling two types of prerequisites differently in pattern rule
Date: Fri, 5 Mar 2010 15:11:08 -0500

On Fri, Mar 5, 2010 at 2:10 PM, Philip Guenther <address@hidden> wrote:

>>     In this situation, I generally do:
>>
>> %$(ABS) : $(LD_SCRIPT) $(OBJS)
>> -       $(CC) $(LDFLAGS) $(TARGET_ARCH) $(filter-out %.x,$^) \
>> +       $(CC) $(LDFLAGS) $(TARGET_ARCH) $(OBJS) \
>>          $(LOADLIBES) $(LDLIBS) -o $@
>>
>>     Rationale: if you later have additional dependencies, they'll also
>> have to be filtered out, and anything that goes into that part of the
>> command line for the link is going to belong in $(OBJS) anyways.
>
> I believe that limits the solution to builds that don't use VPATH to
> find the objects...which is probably Just Fine for this case, as
> finding objects (as opposed to sources) with VPATH is a uncommon
> setup, in my estimation.

    I've generally found that for any non-trivial makefile I've
written, using VPATH was the fast route to madness.  Whenever I've
used it, I've either been bitten by having two files with the same
name in different directories, or with other similar problems.  It can
be particularly subtle and nasty; I've had cases where a stray copy of
a file wound up in the VPATH in front of the legit version of the file
due to pilot error, and once that happens you can spend stupid amounts
of time debugging the makefile before you realize that it's grabbing
gui/background.png instead of newgui/background.png.

    I always use explicit paths in makefiles.

    To take the original example in this thread which (effectively)
generated $(OBJS) as:

OBJS=$(SRCS:.c=.o)

    I would typically do this as:

OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))

OBDIRS=$(dir $(OBJS))

$(OBJDIR)/%.o: %.c | $(OBJDIRS)
    $(CC) $< -o $@

$(OBJDIRS):
    @mkdir -p $@

    That way, you have explicit paths to everything; there are no
search order surprises if you happen to have files with the same name
in different directories.  It also means, by the way, that your clean
rule can be:

.PHONY: clean
clean:
    @rm -rf $(OBJDIR)

    And because $(OBJS) has full paths, the linker (or whatever tool
you're using) has no trouble stitching them all together.

                                                          Todd.

-- 
 Todd Showalter, President,
 Electron Jump Games, Inc.




reply via email to

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