简体   繁体   中英

Why does gnu make ignore missing dependencies for pattern rules?

A minimal example:

%.bar: missing_file.pckl
   @echo executed

When I type make foo.bar , make says "Nothing to be done". Why? The missing_file.pckl does not exist and neither does a rule for it. Shouldn't this raise an error? (I know that you can circumvent this by making the rule static - I just want to understand the reasoning.)

On a side note - I'm trying to understand the logic behind make better -, what's the difference between pattern rules and implicit rules (the terms seem to be used interchangeably)? And what's the reason that pattern rules cannot be made phony? I read that implicit rules are intended to produce files - but to me it makes equally sense to me to use pattern rules for phony targets.

Thanks!

A pattern rule is an implicit rule. But suffix rules are also implicit rules.

There are two basic types of rules in a makefile: explicit rules and implicit rules. Explicit rules consist of rules for explicit targets, and also static pattern rules (this is definitely confusing, that "static pattern rules" are actually explicit, but they are: they are just shorthand for writing lots of explicit rules). Implicit rules consist of pattern rules and suffix rules.

Think of an implicit rule as a template. It doesn't actually define any targets at all. It only gives templates for how a target (which matches the pattern/suffix) could be created. If your makefile contains only implicit rules then when you type make it won't do anything, because your makefile hasn't actually defined any targets... it's only created templates.

It's important to understand that there can be many templates that could build the same target. Even the built-in make rules provide many different ways to build an object file, for example: by using a C compiler, a C++ compiler, a FORTRAN compiler, etc. And users can add their own.

If no explicit rule is found for a target, then make will start searching for an implicit rule. It may look through many of them, and you sure wouldn't want to see an error or message for each one that didn't match!

This pretty much gives the answer to all your questions, but to sum up:

The reason you get this message is that make searched all the implicit rules that could be used to build that target, and it didn't find one that could be used (the one you provided doesn't work because the prerequisite doesn't exist... but make doesn't consider that an error because it's just one way the target could be built. People actually use this very handy feature sometimes,.) So it said, "I don't know how to build that target".

Of course, make could generate more details here. It could say "oh, I happen to know that there's only one implicit rule that could match so I can be more specific about why that one rule didn't work and say that the prerequisite was missing". But that would be confusing if you had two implicit rules that might match. Or, it could list all the possible prerequisites that it couldn't find. Or something. I'm not sure any of those would really be an improvement in general. They would be better in some situations, and super-annoying in other situations.

The reason you can't apply .PHONY to a pattern rule is that a pattern rule isn't a target. Of course, a reasonable enhancement would be to allow .PHONY to take a pattern and then apply phoniness to all targets that match that pattern. But, that enhancement hasn't been implemented for whatever reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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