makefile unexpectedly removes target

前端 未结 2 1674
时光说笑
时光说笑 2020-12-15 05:19

A minimal example:

%.txt: foo.log
    # pass

%.log:
    # pass

Run:

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


        
相关标签:
2条回答
  • 2020-12-15 05:50

    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 讨论(0)
  • 2020-12-15 06:00

    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
    
    0 讨论(0)
提交回复
热议问题