makefile计算差异失败

makefile computing diff fails

当我尝试计算两个文本文件之间的 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 有什么问题?

问题是如果文件不同,diff 通常有一个 non-zero 退出代码。这将导致 make 推断命令失败。简单的解决方法是告诉 makeignore the exit code...

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

编辑: 如果目的是完全消除诊断消息,那么您可以使用诸如...

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