如何在 gnu make 中设置跨目录依赖关系

发布于 2024-10-02 14:04:14 字数 1515 浏览 3 评论 0原文

我在多个目录中使用了一组通用的 make 规则。每个 make 都非常简单:

BASEDIR:=../..
TARGET:=theTarget
include Makefile.include

除非被覆盖,否则我有一个源代码的默认值:

SRC:= $(wildcard *.cpp)

我们希望在一个目录中构建目标文件(而不是源代码),所以:

OBJDIR:= ../obj
BARE:= $(foreach f,$(SRC),$(notdir $(f) )
OBJ:= $(foreach o,$(BARE),$(OBJDIR)/$(o).o )

所以我有 .o 文件,但我需要每个文件一个基于 .cpp 触发规则的人 只要知道目录,这很容易:

$(OBJDIR)/%.o: %.cpp

但是在某些目录中,子目录中存在任意分组的文件。我通过在我的中央 makefile 中对所有这些进行硬编码来暂时解决了这个问题:

$(OBJDIR)/%.o: a/%.cpp
    build...
$(OBJDIR)/%.o: b/%.cpp
    build...

但我想做的是能够在可能的情况下在变量中指定构建目录。有没有什么方法可以设置依赖项

$(OBJDIR)/x.o: a/x.cpp
$(OBJDIR)/y.o: a/y.cpp

$(OBJDIR)/z.o: b/z.cpp

无需手动列出依赖项?

就此而言,我还有依赖项文件 (.d),

$(DEPDIR)/%.d: $(SRC)

我想根据源代码中的内容设置依赖项。 例如: .deps/xd: a/x.cpp g++ -MM -MT$(objdir)/$(notdir $@) $< -o $(DEPDIR)/$(notdir $@ )

有没有一种简单的方法可以生成这些规则,而无需为每个目录编写它们?

这是我首先获得有关生成 .d 文件的信息的地方: http://mad-scientist.net/make/autodep.html#advanced

最终的目标是包含所有 .d 文件并从文件自动生成依赖树。但这是最后一部分。我不知道如何使用 .d 文件。我只需将它们全部包括在内吗?

include $(DEPDIR)/%.d

不起作用。

我可以按照一条规则将它们全部放在一起: cat $(DEP) >$(DEPDIR)/deps.inc

然后

include deps.inc

I've got a common set of make rules being used in multiple directories. Each make is very simple:

BASEDIR:=../..
TARGET:=theTarget
include Makefile.include

Unless overridden, I have a default value for the source code:

SRC:= $(wildcard *.cpp)

We want to build the object files in a directory (not with the sources), so:

OBJDIR:= ../obj
BARE:= $(foreach f,$(SRC),$(notdir $(f) )
OBJ:= $(foreach o,$(BARE),$(OBJDIR)/$(o).o )

So I have the .o files, but I need each one to fire a rule based on the .cpp
As long as the directory is known, this is easy:

$(OBJDIR)/%.o: %.cpp

but in some of the directories, there are arbitrarily grouped files in subdirectories. I temporarily got around this by hardcoding all of them in my central makefile:

$(OBJDIR)/%.o: a/%.cpp
    build...
$(OBJDIR)/%.o: b/%.cpp
    build...

But what I'd like to do is be able to specify the build directories in a variable if possible. Is there any way to set up the dependencies

$(OBJDIR)/x.o: a/x.cpp
$(OBJDIR)/y.o: a/y.cpp

and

$(OBJDIR)/z.o: b/z.cpp

without manually having to list out the dependencies?

For that matter, I also have dependency files (.d)

$(DEPDIR)/%.d: $(SRC)

I want to set up dependencies based on whatever is in the source.
For example:
.deps/x.d: a/x.cpp
g++ -MM -MT$(objdir)/$(notdir $@) $< -o $(DEPDIR)/$(notdir $@ )

Is there an easy way to generate these rules without writing them for each directory?

Here's where I got the information on generating the .d files in the first place:
http://mad-scientist.net/make/autodep.html#advanced

The goal in the end is to include all the .d files and have dependence tree auto-generated from the files. But that is the last part. I don't know how to use the .d files. Do I just include them all?

include $(DEPDIR)/%.d

doesn't work.

I can cat them all together in a rule:
cat $(DEP) >$(DEPDIR)/deps.inc

and then

include deps.inc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-10-09 14:04:14

首先,要处理源目录:

vpath %.cpp a b

$(OBJDIR)/%.o: %.cpp
    build...

然后,如果您愿意,可以生成如下所示的依赖项文件:

# Might as well use DEPDIR, rather than "deps"

$(DEPDIR)/%.d: %.cpp 
    g++ -MM ...

# Note the wildcard. And the '-' at the beginning means
# "don't panic if there aren't any".

-include $(DEPDIR)/*.d

但是您引用的源代码中描述了更好的方法。不要为 .d 文件制定规则,只需按照构建目标文件的相同规则构建它们即可。这样,除非您确实需要它们,否则您不会构建它们。

First, to cope with the source directories:

vpath %.cpp a b

$(OBJDIR)/%.o: %.cpp
    build...

Then if you want you can generate the dependency files like this:

# Might as well use DEPDIR, rather than "deps"

$(DEPDIR)/%.d: %.cpp 
    g++ -MM ...

# Note the wildcard. And the '-' at the beginning means
# "don't panic if there aren't any".

-include $(DEPDIR)/*.d

But there is a better way which is described in the source you cite. Don't make a rule for the .d files, just build them in the same rule that builds the object files. That way you don't build them unless you actually need them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文