2

I have been reading make manual section 4.14 "Generating Prerequisites Automatically" and "Advanced Auto-Dependencies" from this web page.

I think I understand the section from the make manual but there is one thing that I can't wrap my head around on the webpage I linked:

If you think about it, this re-invocation is really unneeded. Since we know some prerequisite of the target changed, we don't really need the updated prerequisite list in this build. We already know that we're going to rebuild the target, and having a more up-to-date list won't affect that decision.

So instead of doing:

  %.P : %.c
          $(MAKEDEPEND)
          @sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' < $*.d > $@; \
             rm -f $*.d; [ -s $@ ] || rm -f $@

  include $(SRCS:.c=.P)

They do:

  %.o : %.c
          @$(MAKEDEPEND)
          $(COMPILE.c) -o $@ $<

  -include $(SRCS:.c=.P)

But if we know a target prerequisite has changed, don't we have to update de dependency list for that target? And isn't that exactly whats done on the first invocation of make? My understanding is that the files included with the include-statement just looks like:

list.o list.P : list.cc list.h debug.h

What am I not getting here?

1 Answer 1

3

The point is that a non-existing or out-of-date object needs no dependency list. It is rebuilt anyway.

Since the dependencies for the .P and the .c files are always identical due to their identical prerequisites, we don't have to keep them separately and just build the dependency file whenever the object is built.

When a target prerequisite has changed, the object is re-built, and the dependency list is updated as a side-effect. We don't need the list in this make run, only in the next, because we re-build the object in this run anyway.

The first invocation of make needs no dependency lists and operates purely on a per-object basis. As a side effect of producing the objects, the dependency lists are produced, and the next make run finds them.

The dependency files look like:

list.o : list.cc list.h debug.h

There is no need to mention the dependency file in there at all, for the above reasons.

4
  • "the .P and the .c files" do you mean .P and .o files?
    – evading
    Commented Feb 13, 2013 at 13:44
  • Also, if the dependency files are not needed to build the objects, what do I need them for? The linking is specified in explicit targets for each executable anyway.
    – evading
    Commented Feb 24, 2013 at 10:12
  • @refuser: Sure, .P and .o files, sorry. You need the dependency files to make sure on the next run that no rebuild is necessary.
    – thiton
    Commented Feb 25, 2013 at 16:44
  • I just realized that the compiler is generating the .P files and as such it shouldn't really need them for compiling in the first place... That took me way to long to get =) Thanks!
    – evading
    Commented Feb 26, 2013 at 13:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.