如果当前目录中已存在名为target的文件,则不会生成.PHONY目标

时间:2014-09-02 07:16:51

标签: makefile

在浏览MakeFiles时,我发现当名为target的文件存在时,即使不使用.PHONY,目标也会被构建。 但是,当我对另一个目标做同样的事情,即干净时,目标就不会建立起来并说“清洁是最新的”#34;这没关系。 我只是想知道当文件存在于当前目录中时为什么要构建另一个目标。

生成文件:

CC:= gcc
CCFLAGS:=-Wall -Wextra
hello: hellomake.o hellofunc.o
        $(CC) $(CCFLAGS) hellomake.c hellofunc.c -o file
hellomake.o : hellomake.c
        $(CC) $(CCFLAGS) -c hellomake.c
hellofunc.o : hellofunc.c
        $(CC) $(CCFLAGS) -c hellofunc.c
clean:
        rm -rf *.o
        rm -rf file

我当前目录的文件名与目标相同,为" hello"。 它应该给出结果"你好是最新的"但它没有这样做,并将输出作为: 打招呼

gcc  -Wall -Wextra  hellomake.c hellofunc.c -o file 

请告诉我们为什么这个建筑物成为目标,而目标不是。目前的目录中的目标和文件已经成为目标。

1 个答案:

答案 0 :(得分:3)

因为make查看最后修改的时间来决定要构建的内容。来自make manual

  

make程序使用makefile数据库和文件的最后修改时间来决定哪些文件需要更新。

命令make检查目标与其先决条件之间的时间关系。如果在目标之后修改了先决条件,则表示目标已过期,即使文件存在,也会触发重建。因此,很可能在目标之后修改了依赖项。

为了避免这种行为,你可以:

  • 使用touch更改目标时间戳。
  • 在调用make -t hello之前使用make --touch hellomake hello。来自docs
‘-t’
‘--touch’
Marks targets as up to date without actually changing them. In other words,
make pretends to update the targets but does not really change their
contents; instead only their modified times are updated.
相关问题