Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running all targets at once in a Makefile

I have a Makefile as below

all:
    bison -d -v parser.y
    flex  -o parser.lex.c parser.lex
    gcc  -o cparser parser.lex.c parser.tab.c -lfl -lm 
clean:
    rm parser.tab.h parser.tab.c parser.output parser.lex.c     

When i ran make in terminal then it only runs target all.

I have tried by adding .PHONY:all clean and .PHONY:clean even though it only runs all . What are the changes i should make in Makefile so that it runs all target ?

System Configurations:
Ubuntu 14.10
GNU Make 4.0

like image 371
sonus21 Avatar asked Mar 20 '15 18:03

sonus21


1 Answers

You want to run all and clean when you just type make? That's usually not useful since that would clean up what you just built (though in your case I see that your clean target doesn't actually do that).

To do this you want something like.

all: cparser clean

cparser:
    bison -d -v parser.y
    flex  -o parser.lex.c parser.lex
    gcc  -o cparser parser.lex.c parser.tab.c -lfl -lm

clean:
    rm parser.tab.h parser.tab.c parser.output parser.lex.c

Though that is still a fairly poor makefile and doesn't take advantage of any of the benefits that make affords you (like intelligently rebuilding things only when their prerequisites change).

I would suggest you probably want something more like this.

.PHONY: all
all: cparser

parser.lex: parser.y
    bison -d -v parser.y

parser.lex.% parser.tab.%: parser.lex
    flex  -o parser.lex.c parser.lex

cparser: parser.lex.c parser.tab.c
    gcc  -o cparser parser.lex.c parser.tab.c -lfl -lm

.PHONY: clean    
clean:
    rm parser.tab.h parser.tab.c parser.output parser.lex.c cparser

Assuming I recall correctly that flex will generate both the parser.lex.c and parser.tab.c files at the same time.

Note also that the prerequisites are chained and that clean now cleans the binary as well. You don't need to run make clean to have things rebuild correctly this way.

like image 118
Etan Reisner Avatar answered Sep 28 '22 09:09

Etan Reisner