Makefile help needed

David Kramer david at thekramers.net
Sat Jul 2 08:33:21 EDT 2005


nmeyers at javalinux.net wrote:
> On Sat, Jul 02, 2005 at 02:16:32AM -0400, David Kramer wrote:
>>My makefile is *mostly* working...
> 
> I'm having a little trouble interpreting this part of your note:
> 
>>all: target1 target2 target3 ../otherdir/troubletarget.o
>>I get "make: *** No rule to make target ../otherdir/troubletarget.o"
>>which I can understand, because it doesn't know where the C file is.

What I meant was, I can understand that it doesn't know to look for the .c 
file in a different directory than the .o file is supposed to go.  But I 
thought there should be a way of telling it "this .o file gets built from 
this .c file" without having to tell it how to make a .o into a .c, since it 
already knows how to do this.

I would rather not duplicate the command to compile a .c into a .o in the 
name of reducing duplication that can lead to inconsistency if it was ever 
changed in one place, but not others.

>>
>>HOWEVER, if I do:
>>all: target1 target2 target3 ../otherdir/troubletarget.o
>>it simply does not try to build it at all.
> 
> ... it seems to be describing two different responses to the same thing.
> 
>>If I change that to
>>../otherdir/troubletarget.o: troubletarget.c
>>	$(CC) $(CFLAGS) -c $< -o $@
>>it works, but I don't see why I have to do that, since it already knows 
>>how to turn a .c into a .o, because earlier in the file I have
>>.c.o:
>>	$(CC) $(CFLAGS) -c $< -o $@
>>which should build it fine,
> 
> The Make manual says:
> 
>   A double-suffix rule is defined by a pair of suffixes: the target
>   suffix and the source suffix. It matches any file whose name ends with
>   the target suffix. The corresponding implicit prerequisite is made by
>   replacing the target suffix with the source suffix in the file name.
>   A two-suffix rule whose target and source suffixes are `.o' and `.c'
>   is equivalent to the pattern rule `%.o : %.c'.
> 
> This suggests to me that it will fail for the same reason...

I now see you are right.

bummer,

> It looks like you've already found a solution to your problem - you just
> don't like it :-).

well, except I have a really good reason for not liking it  ;)

I think the thing to do is:

troubletarget.o: troubletarget.c

../otherdir/troubletarget.o: troubletarget.o
	cp troubletarget.o ../otherdir/troubletarget.o

That seems to solve my problem.  It's unfortunate that I have to cp instead 
of mv, but if I use mv, then it wants to rebuild every time.  If I try a 
symbolic link, it complains "too many levels of indirection.

Oh well.  I would have thought there would be a way to define a generic rule 
where the files were in different directories.  I guess not,

Thanks.



More information about the Discuss mailing list