您如何使用mktemp在makefile中创建目录?
这是一个制造费的代码,说明某人如何在makefile中使用mktemp
TEST=$(shell mktemp -d)
mktemp:
echo $(TEST)
touch $(TEST)/test.txt
ls $(TEST)
cat $(TEST)/test.txt
rm -rf $(TEST)
这是一个示例,输出的示例
❯ make mktemp
echo /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.tQI5EeyW
/var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.tQI5EeyW
touch /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.lVL3N8Rp/test.txt
ls /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.sBW9FzgD
cat /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.Ti53SWSw/test.txt
cat: /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.Ti53SWSw/test.txt: No such file or directory
make: *** [mktemp] Error 1
是期望cat/var/folders/62/wkysd_4n0w57ljl9ycfsd9ycfsd9cc0000 gn/tmp.tmp.tmp.tmp.ti53swswswst.test.test.test.test.test.test.test.test.teast.teast.test. >不会出错。
在这种情况下,如何使用MKTEMP?
This is a Makefile piece of code of how someone may use mktemp in a Makefile
TEST=$(shell mktemp -d)
mktemp:
echo $(TEST)
touch $(TEST)/test.txt
ls $(TEST)
cat $(TEST)/test.txt
rm -rf $(TEST)
This is an example output
❯ make mktemp
echo /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.tQI5EeyW
/var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.tQI5EeyW
touch /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.lVL3N8Rp/test.txt
ls /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.sBW9FzgD
cat /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.Ti53SWSw/test.txt
cat: /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.Ti53SWSw/test.txt: No such file or directory
make: *** [mktemp] Error 1
The expectation is that cat /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.Ti53SWSw/test.txt
would not error.
How can mktemp be used in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此行:
将
test
变量的值设置为字符串$(shell mktemp -d)
。它不会扩展该字符串(不运行shell命令),而只能将字符串保持在原样。现在,每当您以后在makefile中使用该值时,它会进行扩展,这意味着
mktemp
再次运行,并且您获得了不同的值:您想使用
另外,您只需使用shell操作编写配方,而不使用任何制造功能,例如
shell
:默认情况下,请注意配方的每条逻辑行都在单独的外壳中运行,因此为了拥有全部这些行在同一外壳中运行(因此它们可以访问相同的
$ test
变量),您需要使用BackSlash将它们组合到单个逻辑线中。This line:
sets the value of the
TEST
variable to the string$(shell mktemp -d)
. It doesn't expand that string (doesn't run the shell command), it just keeps the string as-is.Now, every time you use that value later in the makefile, it's expanded which means
mktemp
is run again, and you get a different value:You want to use immediate expansion when you assign the variable, so that it is expanded only one time when the makefile is parsed; use:
Alternatively you can just write the recipe using shell operations and not use any make functions like
shell
:Note by default each logical line of a recipe is run in a separate shell, so in order to have all the lines run in the same shell (so they have access to the same
$TEST
variable) you need to use backslash to combine them into a single logical line.