Search code examples
makefilediff

makefile computing diff fails


When I try to compute the diff between two text files:

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

I get this weird (?) error:

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

What's wrong with my makefile?


Solution

  • The issue is that diff typically has a non-zero exit code if the files differ. This will cause make to infer the command failed. The easy fix would be to tell make to ignore the exit code...

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

    Edit: If the aim is to get rid of the diagnostic message entirely then you could just use something like...

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