gnuMake, How to have an environment variable override

The problem with

MYLIB:=$(MYLIB_ENV)
MYLIB?=/.../mylib-1.2.34

is that MYLIB is always defined in the first line, so the second never applies.

The typical approach in this situation is just

MYLIB?=/.../mylib-1.2.34

That way individual developers can specify their own value from the shell, either on the make command line

make MYLIB=...

or in their environment before running make

export MYLIB=...
make

(so they can set it once, e.g. in their shell startup scripts, and forget about it).

If you just run

make

without specifying a value for MYLIB, the default /.../mylib-1.2.34 is used.

Another option is to determine where the Makefile is stored, but that doesn't work in all cases (in particular if the path to the Makefile contains spaces).


It's not very clear what it is exactly that you are after. But in case you want that you need to override the settings of the make variable MYLIB to something different from what it is specified in the Makefile WITHOUT having to edit the Makefile for that, then it is very simple: In your make invocations, specify MYLIB on the commandline with a value of your choice , like as:

make -f yourmakefile yourtarget MYLIB="I_need_my_personal_value"

Another method :

MYLIB := ORIGINAL_VALUE

ifneq '$(MYLIB_ENV)' ''
MYLIB := $(MYLIB_ENV)
endif

Tags:

Gnu Make