Rustin Liu

Iā€™m a passionate software engineer who specializes in distributed systems, build systems, and programming tools.

How to define a block in a Makefile?

We can define a block in a Makefile by using the following syntax:

define <block_name>
    <commands>
endef

At same time, we can define some variables in the block, and use them in the block.

define go_build
 GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 $(GO) build -ldflags "-extldflags \"-static\" $(1)" ./cmd/a
 GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 $(GO) build -ldflags "-extldflags \"-static\" $(1)" ./cmd/b
endef

build:
 $(call go_build,$(GO_LDFLAGS))

We pass the $(GO_LDFLAGS) to the go_build block, and use it in the block.

— Rustin Liu