gpt4 book ai didi

makefile - 如果没有 .PHONY 目标,怎么会让人感到困惑?

转载 作者:行者123 更新时间:2023-12-05 04:16:31 31 4
gpt4 key购买 nike

我正在阅读 make 手册,最后我读到了这一段:

Here is how we could write a make rule for cleaning our example editor:

clean:
rm edit $(objects)

In practice, we might want to write the rule in a somewhat more complicated manner to handle unanticipated situations. We would do this:

.PHONY : clean clean :
-rm edit $(objects)

This prevents make from getting confused by an actual file called clean and causes it to continue in spite of errors from rm. (See Phony Targets, and Errors in Recipes.)

我知道 clean 目标不引用文件系统上的文件,但是如果我省略目标上的 .PHONY 声明,make 会如何混淆声明为 .PHONY?

我会看到一个混淆 make 命令的示例。

P.S.:rm 命令前的负号代表什么?

谢谢。

最佳答案

让我们测试一下。启动您选择的终端,导航到您选择的目录,然后创建一个新目录(用于保存测试文件)。我选择了so-27204300:

$ mkdir so-27204300
$ cd so-27204300

现在创建一个没有 .PHONY 注释的简单 Makefile。我只会显示内容:

clean:
echo "Clean is executing"

执行make命令,你应该得到如下输出:

$ make
echo "Clean is executing"
Clean is executing

现在,创建一个clean 文件(touch clean)并再次执行make。你会得到

$ make
make: `clean' is up to date.

因为有一个文件clean 比目标clean 的输出更新。将 .PHONY clean 添加到 Makefile 以便它读取

.PHONY: clean
clean:
echo "Clean is executing"

现在,即使您的目录中有文件 cleanmake 也会执行目标

$ make
echo "Clean is executing"
Clean is executing

关于您的 PS:更改此 Makefile 使其显示为:

.PHONY: clean
clean:
rm test
echo "Done"

如您所见,那里没有 -,而且当前目录中也没有 test 文件。运行 make 会给你

$ make
rm test
rm: cannot remove ‘test’: No such file or directory
make: *** [clean] Error 1

echo 没有输出。如果 clean 目标是另一个目标的先决条件,那么由于这个错误,另一个目标也不会被构建。

添加 - 以便 Makefile 读取:

.PHONY: clean
clean:
-rm test
echo "Done"

解决这个问题:

$ make
rm test
rm: cannot remove ‘test’: No such file or directory
make: [clean] Error 1 (ignored)
echo "Done"
Done

对于 rm,您还可以使用 -f 让它停止提示错误。但是还有其他命令没有错误参数消音器。


如果你想隐藏正在执行的命令的打印,你可以使用@而不是-

关于makefile - 如果没有 .PHONY 目标,怎么会让人感到困惑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27204300/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com