makefile 计算 diff 失败

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

当我尝试计算两个文本文件之间的

diff
时:

results.txt: file1.txt file2.txt
    diff $(word 1,$^) $(word 2,$^) > $@

我收到这个奇怪的(?)错误:

$ make
diff file1.txt file2.txt > results.txt
makefile:2: recipe for target 'results.txt' failed
make: *** [results.txt] Error 1

我的 makefile 有什么问题?

makefile diff
2个回答
3
投票

问题是,如果文件不同,

diff
通常具有非零退出代码。 这将导致
make
推断命令失败。 简单的解决方法是告诉
make
忽略退出代码...

results.txt: file1.txt file2.txt
    -diff $(word 1,$^) $(word 2,$^) > $@

编辑:如果目标是完全摆脱诊断消息,那么您可以使用类似...

results.txt: file1.txt file2.txt
    diff $(word 1,$^) $(word 2,$^) > $@ || exit 0

0
投票

为了避免消除真正的错误:

results.txt: file1.txt file2.txt
    diff $(word 1,$^) $(word 2,$^) > $@ || test $$? -eq 1

这将在 2 退出状态下失败。请参阅https://www.gnu.org/software/diffutils/manual/html_node/Invoking-diff.html

© www.soinside.com 2019 - 2024. All rights reserved.