gpt4 book ai didi

makefile - 是否可以向另一个 Makefile 添加依赖项?

转载 作者:行者123 更新时间:2023-12-02 21:43:53 25 4
gpt4 key购买 nike

我不是在问是否可以 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

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

最佳答案

在某些情况下,可能只包含另一个Makefile,但在这些情况下,它们可能一开始就被写成一个,所以...如果失败的话,最好的要使依赖项跟踪工作,您可以扩展递归 make 方法 - 您自己的 makefile 无法跟踪 somelib/libsomelib.a 的依赖项,因此您必须询问其他 Makefile每次都为你做。恐怕没有办法解决这个问题。

但是,您可以让自己继续使用隐式规则并将外部库的依赖项跟踪转移到其他 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)

关于makefile - 是否可以向另一个 Makefile 添加依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27886570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com