25

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 have CFLAGS 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?

user
  • 5,335
  • 7
  • 47
  • 63
Jay Walker
  • 251
  • 1
  • 3
  • 3

4 Answers4

31

Doesn't the following work for you?

unexport CFLAGS
3rdparty:
        $(MAKE) -f Makefile.3rdparty
P Shved
  • 96,026
  • 17
  • 121
  • 165
  • It does! I didn't know about the "unexport" keyword, now I do. Thanks! – Jay Walker Feb 02 '10 at 22:51
  • 1
    how do you do this only for the `3rdparty` recipe and not other recipes that might need that variable? – user5359531 Jan 24 '20 at 21:08
  • @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 like `3rdparty: CFLAGS:=`. – fuujuhi Sep 24 '21 at 17:13
3

As of version 3.82 make has an "undefine" directive:

undefine CFLAGS 
num1
  • 4,825
  • 4
  • 31
  • 49
1

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
-6

To unset an Environment variable in linux.

Use:

export -n MY_ENV_VARIABLE
SchmitzIT
  • 9,227
  • 9
  • 65
  • 92