c - 生成文件: Pass a target's dependency as an argument

标签 c linux makefile gnu-make

我想知道是否可以将 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/

相关文章:

latex - 推荐的 latex 构建系统?

python - Makefile 在 Python3 中找不到模块

c - 在 C 中解析简单数字 (ASCII) 文件不起作用

c++ - XORing "Hello World!"切断字符串

linux命令删除csv的最后一列

linux - 我用 iptables 设置了 socket MARK 和 TTL,但是我不能从 getsockopt 读取它?

Linux 跟踪/trace_pipe 文件不可读 (debugfs)

ubuntu - 我正在尝试从 github 创建一个应用程序,但出现错误

c - C中两个整数的快速双向散列

c - 实现主 NTP 服务器(GPS 接收器)