簡體   English   中英

如何使用帶有日期的 makefile 目標?

[英]Howto use makefile target with date?

我想要這樣的東西(目標文件名中的日期(來自shell))

bakfile=mybackup.$(date +%Y%m%d).tar
$(bakfile):
     tar -cf $@ /source-dir

或者可能是目標規范中的日期?

mybackup.$(date +%Y%m%d).tar:
     tar -cf $@ /source-dir

makefile 不是 shell 腳本。 A makefile can contain shell scripts, but you can't just write shell operations directly anywhere in a makefile and expect it to work like the shell.

我建議:

date := $(shell date +%Y%m%d)

然后在需要日期字符串的任何地方使用變量$(date) 多次運行它是一種競爭條件風險,如果你在午夜左右運行它,值可能會在兩次不同的調用之間發生變化。

如果要從 makefile 中調用 shell 命令,則必須付出更大的努力:

bakfile=mybackup.$(shell date +%Y%m%d).tar

您也可以在目標名稱中執行此操作,但我建議不要這樣做——這很難閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM