是否可以将依赖项添加到另一个Makefile?

是否可以将依赖项添加到另一个Makefile?,makefile,Makefile,我不是问是否有可能 假设我有一个生成可执行文件的规则,如下所示: my-prog: some.o local.o dependencies.o # list foreign targets here FOREIGN_TARGETS = \ somelib/libsomelib.a \ foo/libfoo.a \ bar/libbar.a $(FOREIGN_TARGETS): # split the target into directory and file

我不是问是否有可能

假设我有一个生成可执行文件的规则,如下所示:

my-prog: some.o local.o dependencies.o
# 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)
注意,我正在利用这里

现在假设我开始使用第三方库。我希望保留此内置语法,只需将外部规则添加到依赖项列表:

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

但这正是我想要避免的。有什么想法吗?

在某些情况下,可能只
包含另一个Makefile,但在这些情况下,它们很可能是一开始就作为一个Makefile编写的,因此……如果失败,要使依赖项跟踪工作正常,您所能做的最好的事情就是扩展递归make方法——您自己的makefile无法跟踪
somelib/libsomelib.a
的依赖项,因此每次您都必须让另一个makefile为您做这件事。恐怕没办法了

但是,您可以让自己继续使用隐式规则,并将外部lib的依赖项跟踪转移到另一个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
这可以扩展到多个外部目标,如下所示:

my-prog: some.o local.o dependencies.o
# 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)

是的,我认为这是一个相当聪明的解决方案。有没有办法查看
$(MAKE)-C somelib/libsomelib.a
命令的结果,并且只有在somelib
Makefile
中确实触发了目标的更改时,才将目标标记为
已更改