Make: Adding ";" after dependency causes "$<" to not refer to where it was found in "vpath"

Karr, David
Fri Jun 22 14:40:00 GMT 2001

I'm confused by some behavior I'm seeing from GNU make.  It's certainly
possible I'm doing something wrong, but I'm at a loss to understand what is
going on here.

Following this is "Makefile" and "foo.c", which is just a "Hello, world"
program.  I have the source file in a "src" subdirectory, but I refer to the
source file name without the directory name, and use "vpath %.c src" so Make
knows where to find it.  My rule for compiling the source file uses "$<" to
specify the source file name.

These two lines from the Makefile are what concern me.

  #% :: dist ; echo bar
  % :: dist

If I do a "make all" with it in this state, I get the following (assume I
always do a "make clean" before doing anything):

  gcc -o foo.o -c src/foo.c
  gcc -o target.exe foo.o

This works fine.  Notice that "$<" expanded to "src/foo.c".

Now, if in the two lines of "concern" from above, if you comment back in the
first line and comment out the second line, and then do "make all" again,
you get:

  echo bar
  gcc -o foo.o -c foo.c
  gcc: foo.c: No such file or directory
  gcc: No input files
  make: *** [foo.o] Error 1

So, just by adding a command ("echo bar") after the catchall dependency on
"dist", it caused the value of "$<" for the "%.o" rule to be "foo.c" instead
of "src/foo.c".  It turns out it doesn't matter if I do "; echo bar", or if
I just put a "\techo bar" line after "% :: dist".

Then, I noticed that if I change "%.o : %.c" to "%.o :: %.c", then it gives
me the correct behavior again, where "$<" is equal to "src/foo.c".

What is going on here?

vpath %.c src
all : target.exe
dist :
	if [ ! -d $@ ]; then mkdir -p $@; fi

target.exe : foo.o
	gcc -o $@ $^

#%.o :: %.c
%.o : %.c
	gcc -o $@ -c $<

% :: dist ; echo bar
#% :: dist

clean :
	-rm -f *.o target.exe

#include <stdio.h>
int main(int argc, char* argv[])
	printf("Hello, world.\n");

Want to unsubscribe from this list?
Check out:

More information about the Cygwin mailing list