Makefile:将目标的依赖项作为参数传递

时间:2017-11-06 14:23:51

标签: 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

但是,请继续从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

我很乐意帮助解决这个问题。 提前致谢

3 个答案:

答案 0 :(得分:3)

您可以使用变量的命令行覆盖文件功能:

var-dep = dep1.o
target-full : $(var-dep) dep2.o dep3.o

并在命令行上简单地覆盖它:

make var-dep=myother.o

答案 1 :(得分:1)

我认为你所寻找的是动态创建依赖关系:

ifndef dep1-file
target_full: dep1.o
else
target_full: $dep-file
endif

target_full: dep2.o dep3.o
     commands ... $^ ...

这意味着dep1.otarget_full的依赖关系iff dep1-file未定义。 dep1.o仅在它是顶级目标的依赖项时构建。

---------编辑---------------

另一种方法是使用变量来表示所有依赖关系:

DEPS=dep2.o dep3.o
ifndef dep1-file
    DEPS+=dep1.o
else
    DEPS+=$dep1-file
fi

target_full: ${DEPS}
    commands ... $^ ...

答案 2 :(得分:0)

我不明白。如果你说make dep2.o dep3.o make将构建dep2.o和dep3.o.你甚至可以说:

.default:  all
all : target_full

target_full : dep1.o dep2.o dep3.o
    commands

dep1.o : file1 file2
    commands

dep2.o : file3 file4
    commands

dep3.o : file5 file6
    commands

dep1-file : dep2.o dep3.o

如果您现在说make dep1-file只有dep2.o和dep3.o构建其他未触及的