Makefile assignment to special variable .DEFAULT_GOAL is not expanded

Viewed 82
.DEFAULT_GOAL=${IMG}

BUILD_DIR=build

IMG=${BUILD_DIR}/hd60M.img
BIN=${addprefix ${BUILD_DIR}/,${addsuffix .bin,${objects}}}

${IMG}:${BIN}
    touch $@

make -n gives the result:

make: *** No rule to make target `${IMG}'.  Stop.

If I change the first line to .DEFAULT_GOAL=build/hd60M.img, it works without error messages. It seems that assignment to .DEFAULT_GOAL is not expanded. Why?

2 Answers

You should use := instead of =, () instead of {}, and expand variables AFTER setting them:

# Expand rhs on assignment
IMG := $(BUILD_DIR)/hd60M.img

…

# Set default goal after variable is set
.DEFAULT_GOAL := $(IMG)

I find the problem is due to my make version.

$ make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for i386-apple-darwin11.3.0

After I updated GNU make to the newest version, the problem is solved.

Related