2

I'm facing a silly problem with GNU makefile. I want to define two targets to build a c program; one with debugging and the other without.

runNoDebug: setNoDeb objs runMe

runDebug: setDeb objs runMe

setNoDeb:
     {EXPORT} MyDEBUG= -O3

setDeb:
     {EXPORT} MyDEBUG="-DDEBUG=1 -g"

objs: cFiles
    $(CC) -o $@ $^ $(cFiles) $(CFLAGS) $(LIBS) $(MYDEBUG)

runme: objs
    ./oo

Errors arise on running this makefile, the command to set debugging executes on the subshell causing errors. If "Export" is added, the variable is defined in that subshell.

I want to define this variable in the makefile iteself to be used while building objects.

Is it possible? Or should I duplicate the "objs: cFiles" target?

Tarek Eldeeb
  • 588
  • 2
  • 6
  • 24
  • 3
    possible duplicate of [How can I configure my makefile for debug and release builds?](http://stackoverflow.com/questions/1079832/how-can-i-configure-my-makefile-for-debug-and-release-builds) – mmmmmm Jul 24 '12 at 11:38

1 Answers1

2

You need target-specific variable values :

This feature allows you to define different values for the same variable, based on the target that make is currently building.

runNoDebug: setNoDeb runMe

runDebug:   setDeb runMe

setNoDeb:   CFLAGS += -O3
setNoDeb:   objs

setDeb: CPPFLAGS += -DDEBUG=1
setDeb: CFLAGS += -g
setDeb: objs

objs: $(cFiles)
    $(CC) $(CFLAGS) $^ $(LIBS) -o $@
Chnossos
  • 9,971
  • 4
  • 28
  • 40