On Sat, May 15, 2010 at 5:44 PM, Gray Lotus <[email protected]> wrote:
>   I am reading a book on GNU make. There is an example
> in it puzzled me. The makefile is as follows:
...
> LEX=flex
> SOURCE=count_words.c lexer.c counter.c
>
> count_words: counter.o lexer.o -lfl
>
> -include $(subst .c,.d,$(SOURCE))
>
> %.d: %.c
>        $(CC) -M $(CPPFLAGS) $< > $...@.$$$$;      \
>        sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $...@.$$$$ > $@;     \
>        rm -f $...@.$$$$
>
> and it produce the following message:
>
> cc -M -I include src/counter.c > counter.d.$$;  \
>        sed 's,\(counter\)\.o[ :]*,\1.o counter.d : ,g' < counter.d.$$ > 
> counter.d;     \
>        rm -f counter.d.$$
> flex  -t src/lexer.l > lexer.c
> cc -M -I include lexer.c > lexer.d.$$;  \
>        sed 's,\(lexer\)\.o[ :]*,\1.o lexer.d : ,g' < lexer.d.$$ > lexer.d;    
>  \
>        rm -f lexer.d.$$
> cc -M -I include src/count_words.c > count_words.d.$$;  \
>        sed 's,\(count_words\)\.o[ :]*,\1.o count_words.d : ,g' < 
> count_words.d.$$ > count_words.d;     \
>        rm -f count_words.d.$$
> rm lexer.c
> flex  -t src/lexer.l > lexer.c
> cc -M -I include lexer.c > lexer.d.$$;  \
>        sed 's,\(lexer\)\.o[ :]*,\1.o lexer.d : ,g' < lexer.d.$$ > lexer.d;    
>  \
>        rm -f lexer.d.$$
> cc  -I include  -c -o count_words.o src/count_words.c
> cc  -I include  -c -o counter.o src/counter.c
> cc  -I include  -c -o lexer.o lexer.c
> cc   count_words.o counter.o lexer.o /lib/libfl.a   -o count_words
>
> The flex command has executed twice, but the first one has deleted the
> lexer.c, whereas the second not. If they two invoked the same implicit
> rule, why resulting in different effects?

In the first instance, make is trying to build lexer.d, while in the
second it's trying to build lexer.c.  In the former, lexer.c is an
intermediate file and is therefore deleted once that stage of the
build is done.  If you marked lexer.c as a secondary target with

.SECONDARY: lexer.c

then it wouldn't be removed for being an intermediate file.

(BTW, generating dependencies like that makefile does is considered
non-optimal in most cases.  Check out Paul's "Advanced Auto-Dependency
Generation" web page at
    http://make.paulandlesley.org/autodep.html
for details of why and how it can be optimized.)


Philip Guenther

_______________________________________________
Help-make mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-make

Reply via email to