使用make无法从同一输入生成两个文件

时间:2016-10-13 09:26:11

标签: makefile

假设我有一些输入文件sheet[n].in。对于每个输入文件,我想生成两个输出文件sheet[n].outsolved[n].out。我写了这个Makefile:

SOURCES = sheet1.in
OUT = $(SOURCES:.in=.out)
SOLVED = $(patsubst sheet%,solved%,$(OUT))

sheets: ${OUT}
solvedsheets: ${SOLVED} 
all: sheets solvedsheets

sheet%.out: sheet%.in
    cat $< > $@

solved%.out: sheet%.in
    cat <% > $@
    echo "solved!" >> $@

debug:
    @echo "OUT = ${OUT}"
    @echo "SOLVED = ${SOLVED}"

我用一些包含一些文本的虚拟sheet1.in文件测试它。运行make时,它会生成sheet1.out,但不生成solved1.outmake debug收益:

OUT = sheet1.out
SOLVED = solved1.out

我可以删除solved%.out的规则,并且甚至不会抱怨缺少规则。它似乎只是忽略SOLVED列表作为必要的输出文件。那是为什么?

1 个答案:

答案 0 :(得分:1)

当您输入没有规则的make时,Make会运行文件中找到的第一个非模式规则。因此,只会调用您的sheets规则。

您有两个简单的解决方案:

  1. all规则作为Makefile中的第一条规则;
  2. 致电make all而非make
  3. 简化且更健壮的Makefile:

    SOURCE := $(wildcard *.in)
    OUTPUT := $(SOURCE:.in=.out) $(SOURCE:sheet%.in=solved%.out)
    
    .PHONY: all
    
    all: $(OUTPUT)
    
    sheet%.out: sheet%.in
        cat $< > $@
    
    solved%.out: sheet%.in
        cat <% > $@
        echo "solved!" >> $@