26

A minimal example:

%.txt: foo.log
    # pass

%.log:
    # pass

Run:

$ make a.txt --dry-run
# pass
# pass
rm foo.log

Why is the last action rm foo.log? How can I get rid of it?

0

2 Answers 2

30

By default, GNU make removes intermediate files. Since %.txt depends on %.log, make wants to remove the .log file. To prevent that behavior you mark them as precious with .PRECIOUS or .SECONDARY.

.PRECIOUS: foo.log

Also, you can make it so that no intermediate files are ever removed by using .SECONDARY with no dependencies.

.SECONDARY:

See this section of the GNU make manual.

0
7

You are making a.txt by means of a chain of implicit rules (in this case implicit rules which you defined yourself). So Make considers foo.log an intermediate file, and deletes it when it has served its purpose. You can override this behavior by declaring foo.log a SECONDARY target:

.SECONDARY: foo.log
3
  • 2
    .SECONDARY: %.log # doesnt work, what should I do to make the rule general? Mar 25, 2011 at 0:18
  • 7
    If you want all intermediates to remain, just declare a target of .SECONDARY: with no dependencies. Alternatively, you can use the wildcard with .PRECIOUS. i.e. .PRECIOUS: %.log
    – Sean
    Mar 25, 2011 at 0:22
  • 4
    I know but that's what the manual says. If you've ever read the manual then you know that this is not the most surprising inconsistency.
    – Sean
    Mar 25, 2011 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.