Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with this Makefile?

Tags:

makefile

When I run make all on the following Makefile I get this error:

Makefile:5: *** missing separator. Stop.

What's wrong with it and how do I fix it?

LEX = lex
YACC = yacc 
CC = gcc
calcu: y.tab.o lex.yy.o
$(CC) -o calcu y.tab.o lex.yy.o -ly -lfl
y.tab.c y.tab.h: parser.y
$(YACC) -d parser.y
y.tab.o: y.tab.c parser.h
$(CC) -c y.tab.c
lex.yy.o: y.tab.h lex.yy.c 
$(CC) -c lex.yy.c
lex.yy.c: calclexer.l parser.h
$(LEX) calclexer.l
clean:
rm *.o
rm *.c
rm calcu
like image 713
neuromancer Avatar asked Nov 29 '22 04:11

neuromancer


2 Answers

G'day,

You need tabs to indent the lines underneath each target.

LEX = lex
YACC = yacc 
CC = gcc
calcu: y.tab.o lex.yy.o
    $(CC) -o calcu y.tab.o lex.yy.o -ly -lfl
y.tab.c y.tab.h: parser.y
    $(YACC) -d parser.y
y.tab.o: y.tab.c parser.h
    $(CC) -c y.tab.c
lex.yy.o: y.tab.h lex.yy.c 
    $(CC) -c lex.yy.c
lex.yy.c: calclexer.l parser.h
    $(LEX) calclexer.l
clean:
    rm *.o
    rm *.c
    rm calcu

BTW General convention is that you should use braces rather than brackets for your macros. Using brackets are a legacy thing left over from substituting an object back in to an archive. So the above is better expressed as:

LEX = lex
YACC = yacc 
CC = gcc
calcu: y.tab.o lex.yy.o
    ${CC} -o calcu y.tab.o lex.yy.o -ly -lfl
y.tab.c y.tab.h: parser.y
    ${YACC} -d parser.y
y.tab.o: y.tab.c parser.h
    ${CC} -c y.tab.c
lex.yy.o: y.tab.h lex.yy.c 
    ${CC} -c lex.yy.c
lex.yy.c: calclexer.l parser.h
    ${LEX} calclexer.l
clean:
    rm *.o
    rm *.c
    rm calcu

HTH

like image 174
Rob Wells Avatar answered Dec 20 '22 00:12

Rob Wells


You need to indent like so. (Note: they changed my tabs into a 4space. Make sure to indent with the tab character.)

LEX = lex
YACC = yacc 
CC = gcc

calcu: y.tab.o lex.yy.o
    $(CC) -o calcu y.tab.o lex.yy.o -ly -lfl

y.tab.c y.tab.h: parser.y
    $(YACC) -d parser.y

y.tab.o: y.tab.c parser.h
    $(CC) -c y.tab.c

lex.yy.o: y.tab.h lex.yy.c 
    $(CC) -c lex.yy.c

lex.yy.c: calclexer.l parser.h
    $(LEX) calclexer.l

clean:
    rm *.o
    rm *.c
    rm calcu

The extra blank lines aren't required but the indenting is. You'll also need to make an all rule if you want to make all.

like image 29
Paige Ruten Avatar answered Dec 20 '22 02:12

Paige Ruten