为什么makefile坚持编译不应该编译的东西?
所以我运行make lex并生成lex.yy.c文件,一切都很好
然后我运行make scanner,它将一个名为scanner.c的源文件转换为编译,它应该只运行cc lex.yy.c scanner.c -o scanner,但它会这样做: lex -t scanner.l > scanner.c cc lex.yy.c scanner.c -o scanner 为什么它决定运行lex -t scanner.l并将其输出到scanner.c有效地覆盖我的代码?没有该死的想法,这让我发疯了. 我的makefile: scanner: scanner.h scanner.c lex.yy.c cc lex.yy.c scanner.c -o scanner lex: scanner.l lex scanner.l > lex.yy.c clean: rm lex.yy.c rm scanner 出了什么问题? 解决方法
每当发生这种情况时,您在构建目录中都有一个scanner.c 当您运行make scanner时,配方: scanner: scanner.h scanner.c lex.yy.c cc lex.yy.c scanner.c -o scanner 要求其先决条件scan.c应为uptodate.您 通过运行make -p来检查那些内置的配方,你会发现: %.c: %.l # recipe to execute (built-in): @$(RM) $@ $(LEX.l) $< > $@ 这个内置配方将通过运行以下命令从匹配的file.l生成file.c: rm file.c # Not echoed lex -t file.l > file.c Make发现此配方的模式规则 – %.c:%.l – 已满足 lex -t scanner.l > scanner.c 从而破坏了你的扫描仪. 如果你不想让make永远地应用这个内置配方,你可以明确地 %.c: %.l 在没有任何配方的makefile中. 您还可以通过传递–no-builtin-rules来禁用所有内置配方 但是,每当你对makefile的行为有所期望 %.<target-type>: %.<prereq-type> <command> ... 体现了制作< target-type>的规范方式来自< prereq-type>的文件 Make的内置配方为%.c:%.l规则表达了规范的方式 假设你在这个玩具示例中利用了make的内置配方: lexer.l %{ #include <stdio.h> %} %% [ t] ; [0-9]+.[0-9]+ { printf("Found a floating-point number: [%s]n",yytext); } [0-9]+ { printf("Found an integer: [%s]n",yytext); } [a-zA-Z0-9]+ { printf("Found a string: [%s]n",yytext); } %% scanner.c #include "scanner.h" int main(void) { yylex(); return 0; } scanner.h #ifndef SCANNER_H #define SCANNER_H extern int yylex(void); #endif 然后你的makefile可能只是: Makefile文件 SRCS := scanner.c lexer.c OBJS := $(SRCS:.c=.o) LDLIBS := -lfl .PHONY: all clean all: scanner scanner: $(OBJS) $(LINK.o) -o $@ $^ $(LDLIBS) scanner.o: scanner.h clean: rm -f scanner *.o 运行方式如下: $make cc -c -o scanner.o scanner.c lex -t lexer.l > lexer.c cc -c -o lexer.o lexer.c cc -o scanner scanner.o lexer.o -lfl rm lexer.c 请注意,make运行所有这些命令: cc -c -o scanner.o scanner.c lex -t lexer.l > lexer.c cc -c -o lexer.o lexer.c 没有你的lexer.c,lexer.o和scanner.o rm lexer.c 没有被告知. 这个扫描仪运行如下: $./scanner hello Found a string: [hello] 42 Found an integer: [42] 42.42 Found a floating-point number: [42.42] ^C (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |