makefile - 使 : Get dependencies of a target inside another one

标签 makefile

我经常发现自己试图在另一个目标 (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/

相关文章:

c - Makefile:没有链接问题的编译

c - Makefile C 包含已安装库的头文件并链接到库的最佳方法

c 多个源文件返回错误值

c - 简单的 makefile 库变量 - 替换隐式规则

c - makefile:4: *** 缺少分隔符。停止

linux - srlua makefile 错误 lua.h No such file or directory

shell - 在 Makefile 中使用 shell 查找 Ubuntu 版本

c - makefile 编译不正确

makefile - make 中惰性设置和立即设置之间巨大的内存使用差异

linux - 在 shell 脚本中调用 make 之前无法设置环境变量