Tarek Eldeeb
Tarek Eldeeb

Reputation: 606

Setting Variables within Makefile commands

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?

Upvotes: 2

Views: 400

Answers (1)

Chnossos
Chnossos

Reputation: 10456

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 $@

Upvotes: 2

Related Questions