polar9527
polar9527

Reputation: 508

An Implicit rule without Prerequisites in Makefile

directory structure:

src
├── Makefile
├── grpc
│   ├── grpc_demo5_client.go
└── └── grpc_demo5_server.go

Makefile:

groc/test: groc/grpc_demo5_client.go

groc/%:
    echo $@ " - " $^

Output result:

echo groc/grpc_demo5_client.go " - " 
groc/grpc_demo5_client.go  - 
echo groc/test " - " groc/grpc_demo5_client.go
groc/test  -  groc/grpc_demo5_client.go

documentation

An implicit rule can apply to any target that matches its pattern, but it does apply only when the target has no recipe otherwise specified, and only when the prerequisites can be found.

I think that the file groc/grpc_demo5_client.go is not a target.
My question is why echo groc/grpc_demo5_client.go " - " and groc/grpc_demo5_client.go - appear in Output result?

Upvotes: 0

Views: 439

Answers (1)

Eric Backus
Eric Backus

Reputation: 1924

More documentation from the same source as your documentation:

When a target is a file, it needs to be recompiled or relinked if any of its
prerequisites change. In addition, any prerequisites that are themselves automatically
generated should be updated first.

Since groc/test depends on groc/grpc_demo5_client.go, groc/grpc_demo5_client.go becomes a target which must itself be generated. Without this kind of recursive behavior, make would not be nearly as useful.

In still more documentation, it says:

If none of the explicit rules for a target has a recipe, then make searches for
an applicable implicit rule to find one 

Your first rule has no recipe, so the recipe from the second rule is used for groc/grpc_demo5_client.go.

But maybe your problem is that there's typos in your Makefile, and groc should be grpc. Try:

grpc/test: grpc/grpc_demo5_client.go

grpc/%:
    echo $@ " - " $^

Upvotes: 1

Related Questions