【发布时间】:2018-03-26 14:25:43
【问题描述】:
我在使用 Makefiles 生成包含文件的 md5sum 的包时遇到问题。
我确实找到了解决方法,但我对此并不满意。
这是我的 makefile 工作方式的一个示例,它可用于重现我的 file1 问题,以及我使用 file2 的解决方法。
VERSION:=1
DIR:=tmp
file: #rule to build the file with current version tag
touch $(DIR)/file-$(VERSION)
$(DIR)/file1.tar:file #rule that fails to create the md5 file
cd $(DIR)
md5sum -b \
file-$(VERSION) \
>> file-$(VERSION).md5
tar -cf $@ \
file-$(VERSION) \
file-$(VERSION).md5
cd -
$(DIR)/file2.tar:file #workaround that fails to create the md5 file
md5sum -b \
$(DIR)/file-$(VERSION) \
>> $(DIR)/file-$(VERSION).md5
tar -cf $@ -C $(DIR) \
file-$(VERSION) \
file-$(VERSION).md5
file1: $(DIR) $(DIR)/file1.tar
file2: $(DIR) $(DIR)/file2.tar
$(DIR):
mkdir -p $(DIR)
运行file1,构建失败,我得到以下输出:
:~/tmp$ make file1
mkdir -p tmp
touch tmp/file-1
cd tmp
md5sum -b \
file-1 \
>> file-1.md5
md5sum: file-1: No such file or directory
Makefile:8: recipe for target 'tmp/file1.tar' failed
make: *** [tmp/file1.tar] Error 1
运行file2,文件构建成功:
:~/tmp$ make file2
touch tmp/file-1
md5sum -b \
tmp/file-1 \
>> tmp/file-1.md5
tar -cf tmp/file2.tar -C tmp \
file-1 \
file-1.md5
我的问题是,为什么当md5sum 工具用作Makefile 指令时,在调用cd dir 后,它无法在其运行的同一目录中找到文件?或者,我错过了什么?
【问题讨论】:
-
配方中的每一行都由单独的 shell 调用执行。因此,您的
cd $(DIR)行由 shell 执行,并且对由另一个 shell 执行的下一行 (md5sum...) 没有影响。 -
@RenaudPacalet,谢谢。我习惯于看到
cd和cd -总是在同一行,但从没想过这是一个要求。刚刚添加了`; ` 在每条指令的末尾被解释为一行,它现在正在工作。你能回答这个问题以便我接受吗? -
问题已回答。请注意,根据您希望在命令失败时发生的情况,您还可以使用
&&或||链接您的命令。