在 Makefile 依赖项中包含生成的文件

Including generated files in Makefile dependencies

我有一组生成的文件。

GENERATED = log/loga.c log/logb.h include/loga.h

我低于 initproc 的目标取决于上面的 GENERATED。但我不能像 $(INIT_OBJS) 一样在下面包含 $(GENERATED)。它说

fatal error: include/loga.h: No such file or directory 当我做 make initproc

initproc: $(INIT_OBJS) log/loga.o init/initb.o settings/settingc.o
             $(CXX) $(CXXFLAGS)  $(LDFLAGS) $^ $(LDLIBSXX)  -o $@

如何包含上面的依赖项?

您必须添加一个 $(GENERATED) 目标和一些规则来解释如何生成这些文件。此外,您必须使用一些 patsusbt and filter 函数来管理头文件、源文件和目标文件。类似的东西应该可以工作:

# All the generated files (source and header files)
GENERATED := log/loga.c log/logb.h include/loga.h

# The rules to generate this files
$(GENERATED):
        <some commands to generate the files ...>

# The rules to generate object files from the generated source files
# This could be merge with another rule from your Makefile
GENERATED_SRC := $(filter %.c,$(GENERATED))
GENERATED_OBJ := $(patsubst %.c,%.o,$(GENERATED_SRC))

$(GENERATED_OBJ): $(GENERATED_SRC)
        $(CXX) $(CXXFLAGS) -c $^ -o $@

# The final target depends on the generated object files
initproc: $(INIT_OBJS) <other objects ...> $(GENERATED_OBJ)
        $(CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(LDLIBSXX) -o $@