Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Makefile 如果当前目录中已存在名为target的文件,则生成虚假目标_Makefile - Fatal编程技术网

Makefile 如果当前目录中已存在名为target的文件,则生成虚假目标

Makefile 如果当前目录中已存在名为target的文件,则生成虚假目标,makefile,Makefile,在浏览MakeFiles时,我发现当名为target的文件存在时,即使没有使用.PHONY,target也会生成。 但是,当我对另一个目标(即清洁)进行同样的操作时,目标没有得到建立,并且说“清洁是最新的”,这是可以的。 我只是想知道,当文件存在于当前目录中时,为什么要构建另一个目标 生成文件: CC:= gcc CCFLAGS:=-Wall -Wextra hello: hellomake.o hellofunc.o $(CC) $(CCFLAGS) hellomake.c h

在浏览MakeFiles时,我发现当名为target的文件存在时,即使没有使用.PHONY,target也会生成。 但是,当我对另一个目标(即清洁)进行同样的操作时,目标没有得到建立,并且说“清洁是最新的”,这是可以的。 我只是想知道,当文件存在于当前目录中时,为什么要构建另一个目标

生成文件:

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”。 它应该给出“hello是最新的”结果,但它没有这样做,并给出如下输出: 打招呼

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

请说明当目标不是.PHONY且名为target的文件已存在于当前目录中时,此生成为什么是目标。

因为
make
查看上次修改的时间以决定生成什么。从:

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

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

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

  • 用于更改目标时间戳
  • 在调用
    make hello
    之前,使用
    make-t hello
    make--touch hello
    。从:

如果
hellomake.c
hellofunc.c
hello
最新,它将重新生成。运行两次
make hello
你可以在第二次看到它将给出hello是最新的
我只想知道当文件存在于当前目录中时为什么要生成另一个目标
你应该检查目标是否重建,即使依赖项没有修改。创建我在makefile,我正在我的目录中创建“hello”文件。但makefile仍然不会给出任何错误,即使它没有.PHONY。通常,当名为与目标相同的文件出现在当前目录中时,如果不使用.PHONY,则不应运行目标的命令,但在我的示例中,它正在这样做。:)避免这种行为的最好方法是简单地使用配方创建的文件名作为目标,这就是make的工作方式。因此,如果您的配方像上面的配方那样构建了一个名为
file
的文件(
gcc…-o file
),那么目标名称应该是
file
,而不是
hello
。然后一切都会如预期的那样。
‘-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.