makefile实例

count_word.c

#include <stdio.h>
extern int fee_count,fie_count,foe_count,fum_count;
extern int yylex(void);

int main()
{
	yylex();
	printf("%d %d %d %d \n",fee_count,fie_count,foe_count,fum_count);
	//exit(0);
	return 0;


}

lexer.l

	int fee_count = 0;
	int fie_count = 0;
	int foe_count = 0;
	int fum_count = 0;
%%
fee	fee_count++;
fie	fie_count++;
foe	foe_count++;
fum	fum_count++;
1、完全的makefile

count_word: count_word.o lexer.o -lfl
	gcc count_word.o lexer.o -lfl -o count_word
count_word.o: count_word.c
	gcc -c count_word.c
lexer.o: lexer.c
	gcc -c lexer.c
lexer.c: lexer.l
	flex -t lexer.l > lexer.c

clean: 
	rm -rf *.o
	rm -rf count_word
	rm -rf lexer.c

2、模式规则makefile

count_word:count_word.o lexer.o -lfl
count_word.o:count_word.c
lexer.o:lexer.c
lexer.c:lexer.l

clean: 
	rm -rf *.o
	rm -rf count_word
	rm -rf lexer.c

3、自动变量makefile

count_word:count_word.o lexer.o -lfl
	gcc $^ -o $@
count_word.o:count_word.c
	gcc -c $<
lexer.o:lexer.c
	gcc -c $<
lexer.c:lexer.l
	flex -t $< > $@

clean: 
	rm -rf *.o
	rm -rf count_word
	rm -rf lexer.c
测试自动变量可以使用:echo $@




你可能感兴趣的:(makefile实例)