我不是在问call Makefile from another Makefile是否可能。
假设我有一个生成可执行文件的规则,如下所示:
my-prog: some.o local.o dependencies.o
请注意,我在这里利用built-in rules。
现在假设我开始使用第三方库。我想保留这种内置语法,只需将外部规则添加到依赖项列表中:
my-prog: some.o local.o dependencies.o somelib/libsomelib.a
但这不起作用:
No rule to make target 'somelib/libsomelib.a', needed by 'my-prog'.
我知道我可以通过显式调用另一个Makefile来解决这个问题:
my-prog: some.o local.o dependencies.o
$(MAKE) -C somelib/ libsomelib.a
$(CC) $(LDFLAGS) -o $@ $^ somelib/libsomelib.a
但这就是我想要避免的。有什么想法吗?
答案 0 :(得分:3)
在特定情况下,可能只有include
另一个Makefile,但在这些情况下,它们可能首先被写成一个,所以...如果失败,那么你可以做的最好使依赖关系跟踪工作是扩展递归make方法 - 你自己的makefile不能跟踪somelib/libsomelib.a
的依赖关系,所以你必须要求其他Makefile每次都为你做。我担心没有办法解决这个问题。
somelib/libsomelib.a:
$(MAKE) -C somelib/ libsomelib.a
# This target needs to be phony so it is run every time because only the other
# makefile can determine that there's nothing to be done.
.PHONY: somelib/libsomelib.a
# then you can use it as a dependency just like locally built targets
my-prog: some.o local.o dependencies.o somelib/libsomelib.a
这可以扩展到多个外国目标,如:
# list foreign targets here
FOREIGN_TARGETS = \
somelib/libsomelib.a \
foo/libfoo.a \
bar/libbar.a
$(FOREIGN_TARGETS):
# split the target into directory and file path. This assumes that all
# targets directory/filename are built with $(MAKE) -C directory filename
$(MAKE) -C $(dir $@) $(notdir $@)
.PHONY: $(FOREIGN_TARGETS)