Is it possible to "unset" an environment variable in a Makefile?
Solution 1
Doesn't the following work for you?
unexport CFLAGS
3rdparty:
$(MAKE) -f Makefile.3rdparty
Solution 2
As of version 3.82 make has an "undefine" directive:
undefine CFLAGS
Solution 3
This can be problematic if you need the variable to be defined for other commands in the recipe and only don't want it defined in the submake. Another solution is to use env - to invoke the submake and explicitly set any environment variables you need set in the submake, eg:
env - PATH="$$PATH" LD_LIBRARY_PATH="$$LD_LIBRARY_PATH" $(MAKE) -f Makefile.3rdparty
Related videos on Youtube
Jay Walker
Updated on April 16, 2022Comments
-
Jay Walker about 2 years
I'm using GNU make, and including a 3rd party library in a project that has a build system that goes berserk if
CFLAGS
is defined in the environment when it is called. I like to haveCFLAGS
defined in my environment for other reasons. The library's build is being invoked from another makefile, so that I say e.g.:3rdparty:
$(MAKE) -f Makefile.3rdparty
But I would like to be sure that
CFLAGS
is unset when I invoke make on the 3rd party Makefile. The nearest thing I can find is to say:CFLAGS:=
But this still leaves
CFLAGS
set in the environment, it's just an empty string. Apart from doing something hideous like saying:3rdparty:
bash -c "unset CFLAGS; $(MAKE) -f Makefile.3rdparty"
Is there an easy way to "unset" the
CFLAGS
variable from within my primary makefile, so that it isn't present at all in the environment when the third party library is invoked? -
Jay Walker about 14 yearsIt does! I didn't know about the "unexport" keyword, now I do. Thanks!
-
Mike D over 4 yearsdid not unexport environment variables for me
-
user5359531 over 4 yearshow do you do this only for the
3rdparty
recipe and not other recipes that might need that variable? -
fuujuhi over 2 years@user5359531 The only method I see is either start each recipe line with
unset CFLAGS && ...
, or use.ONESHELL´ and add
unset CFLAGS` as first recipe line. If however having the variable empty is ok, you can also add a target specific var define like3rdparty: CFLAGS:=
.