我想知道哪种解决方案更好地在 Makefile 中添加两个数字。就我而言,我将使用add如下所示的函数:
result = $(call add, 34, 56)
$(error $(result))
Run Code Online (Sandbox Code Playgroud)
解决方案1:
add = $(shell echo $$(( $(1) + $(2) )))
Run Code Online (Sandbox Code Playgroud)
解决方案2:
add = $(shell perl -e 'print $1 + $2')
Run Code Online (Sandbox Code Playgroud)
解决方案3:
add = $(shell echo '$1 + $2' | bc | tr '\n' ' ')
Run Code Online (Sandbox Code Playgroud)
解决方案4:
16 := x x x x x x x x x x x x x x x
_input_int := $(foreach a,$(16),$(foreach b,$(16),$(foreach c,$(16),$(16)))))
_decode = $(words $1)
_encode = $(wordlist 1,$1,$(_input_int))
_plus = $1 $2
_max = $(subst xx,x,$(join $1,$2))
_push = $(eval stack := $$1 $(stack))
_pop = $(word 1,$(stack))$(eval stack := $(wordlist 2,$(words $(stack)),$(stack)))
_pope = $(call _encode,$(call _pop))
_pushd = $(call _push,$(call _decode,$1))
calculate=$(eval stack:=)$(foreach t,$1,$(call handle,$t))$(stack)
handle =$(call _pushd, \
$(if $(filter +,$1), \
$(call _plus,$(call _pope),$(call _pope)), \
$(call _encode,$1)))
add = $(strip $(foreach v,$(2), $(call calculate, $v $(1) +)))
Run Code Online (Sandbox Code Playgroud)
我承认解决方案 4 很荒谬,但它是唯一一个不依赖于诸如bash、perl或 之类的外部工具的解决方案bc。