即使没有任何更改,Makefile也会编译文件

问题描述 投票:0回答:1

我正在尝试为我正在编写的小shell创建一个makefile。文件是:

myShell.c
myShellFunc.c
parser.y
shel.l

makefile:

CC = gcc
CFLAGS= -g -Wall


myshell: myShell.o myShellFunc.o parser.tab.o lex.yy.o
    $(CC) $(CFLAGS) myShell.o myShellFunc.o parser.tab.o lex.yy.o -lfl -o myshell

myShell.o: myShell.c myShell.h parser.tab.h
myShellFunc.o: myShellFunc.c myShell.h

parser.tab.c: bisonfiles
parser.tab.h: bisonfiles
lex.yy.c: shell.l
    flex shell.l

.PHONY: bisonfiles
bisonfiles: parser.y
    bison -d parser.y

问题是,即使文件没有变化,它仍会编译它们。

当我第一次运行make时,我明白了:

bison -d parser.y
gcc -g -Wall   -c -o myShell.o myShell.c
gcc -g -Wall   -c -o parser.tab.o parser.tab.c
flex shell.l
gcc -g -Wall   -c -o lex.yy.o lex.yy.c
gcc -g -Wall myShell.o myShellFunc.o parser.tab.o lex.yy.o -lfl -o myshell

我第二次运行它,没有改变任何东西:

bison -d parser.y
gcc -g -Wall   -c -o parser.tab.o parser.tab.c
gcc -g -Wall myShell.o myShellFunc.o parser.tab.o lex.yy.o -lfl -o myshell

我第三次运行它而没有改变任何东西:

bison -d parser.y
gcc -g -Wall   -c -o myShell.o myShell.c
gcc -g -Wall   -c -o parser.tab.o parser.tab.c
gcc -g -Wall myShell.o myShellFunc.o parser.tab.o lex.yy.o -lfl -o myshell

当我没有做出任何改变时,为什么会发生这种情况? 如何正确编写makefile?

makefile
1个回答
0
投票

来自GNU make documentation ......

伪目标不应该是真实目标文件的先决条件;如果是,每次make更新该文件时都会运行其配方。

但是你有...

parser.tab.c: bisonfiles
parser.tab.h: bisonfiles
lex.yy.c: shell.l
        flex shell.l

.PHONY: bisonfiles
bisonfiles: parser.y
        bison -d parser.y

在这里bisonfiles已被宣布为.PHONY目标,因此,你看到bison -d parser.y在每个make调用上运行。

最简单的解决方案可能是完全摆脱bisonfiles目标,只使用模式规则......

%.tab.c %.tab.h: %.y
        bison -d $<
© www.soinside.com 2019 - 2024. All rights reserved.