c - 生成的依赖项与生成的文件

标签 c makefile code-generation gnu-make

我想对从 flexbison 生成的文件使用自动依赖项生成。

现在,我正在这样做:

CSRC=$(wildcard *.c)
OSRC=grammar.y scanner.l
OUTPUT_DIR="../bin"

SOLIDC_OBJS=solid_ast.o solid_strbuf.o solid_strlit.o solidc.o 
SOLIDL_OBJS=solidl.o solid_ast.o solid_strbuf.o

CFLAGS += -MD -MP
CC=clang
BISON=bison
FLEX=flex
MKDIR=mkdir -p

all: solidc

solidc: setup $(SOLIDC_OBJS)
    $(BISON) -vd grammar.y
    $(FLEX) --header-file=scanner.yy.h -o scanner.yy.c scanner.l
    $(CC) -o $(OUTPUT_DIR)/$@ $(SOLIDC_OBJS) scanner.yy.o grammar.tab.o

setup:
    @$(MKDIR) $(OUTPUT_DIR)

run:
    $(OUTPUT_DIR)/$(OUTPUT_NAME)

clean:
    $(RM) $(wildcard $(OUTPUT_DIR)/*) $(wildcard *.o) $(wildcard *.d) \
        $(wildcard scanner.yy.*) $(wildcard grammar.tab.*) \
        $(wildcard grammar.output)

-include $(SRC:%.c=%.d) scanner.yy.d grammar.tab.d

但这不起作用,因为 scanner.yy.dgrammar.tab.d 永远没有机会生成。

解决此问题的最佳方法是什么?


Please note that this question is actually not the same as GNU make: Generating automatic dependencies with generated header files, because I'm using the "less efficient" -include method rather than sef, in addition to lots of other things.

最佳答案

如果我没理解错的话,你可以将 grammar.tab.oscanner.yy.o 作为 solidc 的先决条件,然后然后对象规则将为它们生成依赖文件,与任何对象相同。

SOLIDC_OBJS=solid_ast.o solid_strbuf.o solid_strlit.o solidc.o grammar.tab.o scanner.yy.o

all: solidc

grammar.tab.c: grammar.y
    $(BISON) -vd grammar.y

scanner.yy.c: scanner.l
    $(FLEX) --header-file=scanner.yy.h -o scanner.yy.c scanner.l

solidc: setup $(SOLIDC_OBJS)
    $(CC) -o $(OUTPUT_DIR)/$@ $(SOLIDC_OBJS)

(我们可以稍微收紧这些规则,但让我们先让它们发挥作用。)

关于c - 生成的依赖项与生成的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11162456/

相关文章:

c - 为程序员学习 C

java - 是否有任何工具或库可以将 YAML 或 XML 文件转换为 Java 代码?

oracle - 如何使用 PL/SQL 在 Oracle 中创建具有随机字段数的表?

c - gdb 按行号反汇编

c - 为什么这段代码不计算字符数?

android - 通过命令行(Makefile)手动创建android apk

c++ - make 失败,返回错误 "cannot convert ‘std::istream {aka std::basic_istream<char>}’ 到 ‘bool’”

namespaces - 是否可以强制 T4 运行时模板的底层类没有命名空间?

C 错误 : comparison between pointer and integer [enabled by default]

c - 带开罗的 Gouraud 阴影三角形