可以在 gnu-make 的定义中声明规则吗?

Can a rule be declared in a define in gnu-make?

我认为我可以在 make 中使用 define 指令来制定多个相似的规则

define TESTPRG

: 
        echo Build 

endef

$(call TESTPRG,x)

但我得到了这个输出

$ make
make: *** No rule to make target 'echo', needed by '
x'.  Stop.

看来我定义中的换行符丢失了

$ make -n -p | grep echo
make: *** No rule to make target 'echo', needed by '
x'.  Stop.
    echo Build 
x: echo Build x
echo:

我只是想做一些 define 不应该用于的事情。

凯氏

这正是 define 的用途。你只需要记住 $(eval) 它作为 Makefile 的一部分:

$ cat Makefile
define TESTPRG

:
        echo Build 

endef

$(eval $(call TESTPRG,x))

$ make
echo Build x
Build x