简体   繁体   English

Makefile:即使存在目标和依赖项也没有目标错误

[英]Makefile: no target error even when target and dependency exist

My makefile:我的 makefile:

./corpus/%.spacy : ./assets/%.json
     python3 ./scripts/convert.py $< $@

Two questions:两个问题:

  1. Even if A.spacy and A.json exist and A.json is updated more recently than A.spacy, I get the following output.即使存在 A.spacy 和 A.json 并且 A.json 比 A.spacy 最近更新,我得到以下 output。

     $ make $ make: *** No targets. Stop.
  2. What to add to have it make A.spacy if only A.json exists?如果只有 A.json 存在,要添加什么让它制作 A.spacy? I tried the below code, which didn't work and I feel I'm not fully understanding targets and dependencies in makefiles.我尝试了下面的代码,但没有成功,我觉得我没有完全理解 makefile 中的目标和依赖关系。

     convert: ./corpus/%.spacy python3./scripts/convert.py $(./scripts/$*.json) $<./corpus/%.spacy: ./assets/%.json echo $?

Didn't work as in gave the following output没有工作,因为给出了以下 output

$ make convert
$ make: *** No rule to make target `corpus/%.spacy', needed by `convert'.  Stop.

You seem to be thinking that declaring a pattern rule will cause make to go spelunking your directory looking for all possible ways to use that pattern, as if it were a wildcard or something akin to ls *.spacy .您似乎在想,声明一个模式规则会导致 go 在您的目录中寻找使用该模式的所有可能方法,就好像它是通配符或类似于ls *.spacy的东西。

That's not what a pattern rule is.这不是模式规则。

A pattern rule is a template that make can apply if it wants to build a given target and it doesn't know how to build that target.模式规则是一个模板如果它想要构建给定的目标并且它不知道如何构建该目标,则可以应用它。

If you have the above makefile it just tells make "hey, if you happened to want to create a target that matches the pattern ./corpus/%.spacy then here's a way to do it".如果你有上面的 makefile 它只是告诉 make “嘿,如果你碰巧想要创建一个与模式匹配的目标./corpus/%.spacy那么这是一种方法”。 If you type make with no arguments, then you haven't told make that you want to build anything so it won't use your pattern rule.如果您键入make时没有 arguments,那么您还没有告诉 make 您要构建任何东西,因此它不会使用您的模式规则。

If you type:如果您键入:

$ make ./corpus/A.spacy

now you've told make you want to actually build something ( ./corpus/A.spacy ), so now make will try to build that thing, and it will see your pattern rule and it will try to use it.现在你已经告诉 make 你想要实际构建一些东西( ./corpus/A.spacy ),所以现在 make 将尝试构建那个东西,它会看到你的模式规则并尝试使用它。

As for the other, this:至于另一个,这个:

convert : ./corpus/%.spacy
        python3 ./scripts/convert.py $(./scripts/$*.json) $<

is not a pattern rule.不是模式规则。 A pattern rule must have a pattern character ( % ) in the target ;模式规则的目标中必须有模式字符 ( % ); this is defining a target convert that depends on a file named, explicitly, ./corpus/%.spacy of which you don't have any file with that name, so you get that error.这是定义一个目标convert ,它依赖于一个明确命名为./corpus/%.spacy的文件,您没有任何具有该名称的文件,因此您会收到该错误。

You didn't actually describe what you wanted to do, but I think maybe you want to do something like this:你实际上并没有描述你想做什么,但我也许你想做这样的事情:

# Find all the .json files
JSONS := $(wildcard ./corpus/*.json)

# Now figure out all the output files we want
SPACYS := $(patsubst ./corpus/%.json,./corpus/%.spacy,$(JSONS))

# Now create a target that depends on the stuff we want to create
all: $(SPACYS)

# And here's a pattern that tells make how to create ONE spacy file:
./corpus/%.spacy : ./assets/%.json
        python3 ./scripts/convert.py $< $@

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM