我经常发现自己试图在另一个目标 (Target2) 中引用一个目标 (Target1) 的依赖关系。
考虑以下 Makefile。如果有一个 $(deps target-name)
函数该多好啊!
Rule1 : file1 file2 file3
echo $^ # Careful, these are whitespaces, not a tab !
Rule2 : file4 file5 file6
echo $^ # Careful, these are whitespaces, not a tab !
clean-Rule-1-2 :
rm $(deps Rule1) $(deps Rule2) # Careful, these are whitespaces, not a tab !
我找到了this链接提到人们可以为自己构建自己的依赖项列表,但这看起来相当乏味。
是否有人有更好的解决方案(假设 Makefile 中没有本地实现)和/或涉及此问题的工作流程提示?
最佳答案
为什么要列出 clean
类型规则的先决条件?这只会强制 make 构建这些过时的依赖项,然后再次删除它们。
没有办法做你想做的事,因为不可能保持一致。例如,您的规则可以这样写:
Rule1 : file1 file2 file3
echo $^ # Careful, these are whitespaces, not a tab !
clean-Rule-1-2 : $(deps Rule1)
rm $^ # Careful, these are whitespaces, not a tab !
Rule1 : file10 file11 file12
哎呀!当 $(deps ...)
展开时, make 不知道额外的三个先决条件,因此不会列出它们。这甚至没有考虑隐式规则,即 make 在解析 makefile 时不知道完整的先决条件列表是什么;它仅在尝试构建目标时计算它们。
您没有给出您想要做什么的真实示例,但通常编写 makefile 的方式是将先决条件放入变量中,然后您可以在多个地方使用这些变量。
关于makefile - 使 : Get dependencies of a target inside another one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23756212/