我想知道是否可以将 makefile 目标的依赖项作为参数传递。如果可能的话,如何做。
例如,假设我有一个从多个依赖项构建的应用程序
target_full : dep1.o dep2.o dep3.o
commands
dep1.o : file1 file2
commands
dep2.o : file3 file4
commands
dep3.o : file5 file6
commands
我希望能够将已编译的依赖项作为参数传递。
因此,如果我运行 make all
它将从文件生成 dep1.o dep2.o dep3.o 但如果我运行 make all dep1-file=myDep.o
它将仅生成 dep2.o 和 dep3.o 并使用给定的依赖项,而无需从 file1 和 file2 重新编译 dep1.o。
事实上,我根本不想调用第一个依赖目标
我想象过类似的事情
ifndef(dep1-file)
dep1=dep1.o
else
dep1=${dep1-file}
endif
target_full : ${dep1} dep2.o dep3.o
commands
${dep1} : file1 file2
commands
dep2.o : file3 file4
commands
dep3.o : file5 file6
commands
但是 make 继续从 file1 和 file2 编译 dep1.o。也许是因为修改日期,但我也希望不要将其计入在内,因为我不关心 file1 和 file2 是否比我的自定义依赖项更新。
编辑:我应该明确指出我有一个可行的解决方案,但它确实很难看,所以我对它并不完全满意,我认为有更好的方法它是否存在。
但是我将其粘贴到此处,以便它可以帮助理解我的问题。
ifndef dep1-file
dep1=dep1.o
else
dep1=${dep1-file}
endif
target_full : ${dep1} dep2.o dep3.o
commands
ifndef dep1-file
${dep1} : file1 file2
commands
endif
dep2.o : file3 file4
commands
dep3.o : file5 file6
commands
我很高兴能得到一些帮助来解决这个问题。 提前致谢
最佳答案
您可以使用变量的命令行覆盖文件功能:
var-dep = dep1.o
target-full : $(var-dep) dep2.o dep3.o
并简单地在命令行上覆盖它:
make var-dep=myother.o
关于c - 生成文件: Pass a target's dependency as an argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47139080/