I have this Makefile:
CFLAGS := $(CFLAGS) -std=c99
shell: main.o shellparser.o shellscanner.o
$(CC) -o shell main.o shellparser.o shellscanner.o
main.o: main.c shellparser.h shellscanner.h
shellparser.o: shellparser.h
shellparser.h: shellparser.y lemon
./lemon shellparser.y
shellscanner.o: shellscanner.h
shellscanner.h: shellscanner.l
flex --outfile=shellscanner.c --header-file=shellscanner.h shellscanner.l
# Prevent yacc from trying to build parsers.
# http://stackoverflow.com/a/5395195/79202
%.c: %.y
lemon: lemon.c
$(CC) -o lemon lemon.c
For some reason, on the first run of make, shellparser.o isn’t built:
> make
cc -o lemon lemon.c
./lemon shellparser.y
flex --outfile=shellscanner.c --header-file=shellscanner.h shellscanner.l
cc -std=c99 -c -o main.o main.c
cc -std=c99 -c -o shellscanner.o shellscanner.c
cc -o shell main.o shellparser.o shellscanner.o
i686-apple-darwin10-gcc-4.2.1: shellparser.o: No such file or directory
make: *** [shell] Error 1
rm shellscanner.c
If I run it again, it then builds it correctly:
> make
cc -std=c99 -c -o shellparser.o shellparser.c
cc -o shell main.o shellparser.o shellscanner.o
So what do I have out-of-order such that it doesn’t build it the first time?
The first time you try to build, Make doesn’t know that
lemonoutputsshellparser.c, so it doesn’t try to build it. When you rebuild,shellparser.cdoes exist, so Make uses it. The solution is to explicitly tell Make thatlemonoutputsshellparser.c:Also I renamed
getlineso it would build on my mac; thanks for posting all your source!