How can eval function be used in makefile?

In the manual:

The eval function is very special: it allows you to define new makefile constructors that are not constants; which result from evaluating other variables and functions. The argument to the eval function expands, then the results of this decomposition are parsed as a makefile.

It is important to understand that the eval argument expands twice; first using the eval function, the results of this extension are expanded again when they are parsed as makefile syntax. This means that you may need to provide additional escapes for the "$" characters when using eval.

"extended twice" confuses me.

For example, I create a make file:

define func
    tmp = $(OBJPATH)/$(strip $1)
    objs += $$(tmp)
    $$(tmp) : $2
        gcc $$^ -o $$@
endef

all : foo

$(eval $(call func, foo, 1.c))    

How will the eval function expand?

+5
1

- eval :

$(info $(call func, foo, 1.c))

, , make . OBJPATH, obj , ( ) :

tmp = obj/foo
objs += $(tmp)
$(tmp) : 1.c
    gcc $^ -o $@

make , , , $(tmp).

+15

All Articles