【发布时间】:2018-07-04 18:13:01
【问题描述】:
基于this famous link并改编自this gist,假设你所有的源文件都是.cpp,你很容易得到这样的解决方案来生成自动依赖:
SRCS := $(wildcard *.cpp */*.cpp)
DEPDIR := .d
DEPS := $(SRCS:%.cpp=$(DEPDIR)/%.d)
# Temporary .Td dependence file... ?
DEPFLAGS = -MT $@ -MD -MP -MF $(DEPDIR)/$*.Td
# Isn't it better a order-only prerequisite?
$(shell mkdir -p $(dir $(DEPS)) >/dev/null)
%.o: %.cpp # Removal of implicit rules... ? Twice?
%.o: %.cpp $(DEPDIR)/%.d # Dependency on .d... ?
g++ -o $@ $(DEPFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c $<
# Avoid some bugs?
mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d && touch $@
# If the `%.d` dependency is removed, is this still necessary?
.PRECIOUS = $(DEPDIR)/%.d
$(DEPDIR)/%.d: ;
-include $(DEPS)
为了不要让这个问题太长,深入讨论为什么我认为上面sn-p中注释的所有行都是不必要的,我会用简短的形式问它;如果我只是将这个 sn-p 更改为:
SRCS := $(wildcard *.cpp */*.cpp)
DEPDIR := .d
DEPS := $(SRCS:%.cpp=$(DEPDIR)/%.d)
DEPFLAGS := -MT $@ -MD -MP -MF $(DEPDIR)/$*.d
$(DEPDIR)/%:
mkdir -p $@
%.o: %.cpp | $(DEPDIR)/$(dir %)
g++ -o $(DEPFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c $<
# touch $(DEPDIR)/$.d # ??
-include $(DEPS)
我还有两个额外的疑问;在上面的第一个链接中,它说
据报道,某些版本的 GCC 可能会留下目标文件 比依赖文件旧,这会导致不必要的重建。
但是,在gist(上面的第二个链接)中,touch 命令被删除,并且由于依赖文件不再是任何规则的先决条件,是否有任何理由继续包含它?这个“gcc bug”是否仍然适用于任何形式?
第二个疑问与目录创建有关,移至仅订单规则;我是否需要制定“仅限订单”$(DEPDIR)/% 规则.PRECIOUS?如果%.o 配方失败,我不知道make 是否会尝试删除目录,因为我不知道仅订购规则的具体功能。
【问题讨论】:
标签: makefile dependencies auto-generate