[comp.unix.questions] Makefile inference rule

osbornk@mist.cs.orst.edu (Kasey S. Osborn) (06/07/90)

I am trying to establish an inference rule to build executables from
objects.  Here is a highlight of my makefile:

  OBJS = gc.o mem.o

  .c.o:
          cc -ansi -g -c -w0,-Wall $<           # This works fine.

  .o:
          cc -g $(OBJS) $< -o $@                # This is ignored!!!

  box:    $(OBJS) box.o

  box.o:  box.c gc.h

The key here is the .o: inference rule.  It is completely ignored, yet
documentation I read tells me this is how it is done.  I get no diagnostic
message, just a new prompt.  Ironically, if I remove the line:

  box:    $(OBJS) box.o

and invoke "make box", the .o: inference rule takes effect.  However,
the additional dependencies (OBJS) are ignored.

Does anyone have a clue?

Kasey S. Osborn
"There's a lot to be said for brevity."

cpcahil@virtech.uucp (Conor P. Cahill) (06/08/90)

In article <18769@orstcs.CS.ORST.EDU> osbornk@cs.orst.edu (Kasey S. Osborn) writes:
>  .o:
>          cc -g $(OBJS) $< -o $@                # This is ignored!!!
>
>  box:    $(OBJS) box.o

First off, the NULL suffix is only used when you have a single source
for a single object.  In this case you do not meet that condition.

make is actually executing the commands that follow the line

	box:	$(OBJS) box.o

which happen to be nothing.

 
>The key here is the .o: inference rule.  It is completely ignored, yet

It is not ignored, it just doesn't apply.

>Ironically, if I remove the line:
>
>  box:    $(OBJS) box.o
>
>and invoke "make box", the .o: inference rule takes effect.  However,
>the additional dependencies (OBJS) are ignored.

That is because make then thinks that you have a single source file
makeing a program (box.o --> box) and the inference rule applies.

To make a program with more than one object file dependency you 
must have the commands for building that target following the
target/dependency line.  For  your example this would be:

	box:	$(OBJS) box.o
		$(CC) -g $(OBJS) box.o -o $@ 

If all you are trying to do is use the $OBJS to make more than one
program you could just define the following:

MAKE_PGM_FROM_OBJS = $(CC) -g $(OBJS) -o $@ 

And then your box make would be:

	box:	$(OBJS) box.o
		$(MAKE_PGM_FROM_OBJS) box.o

Hope this helps.
-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170