如何在Makefile中正确地添加两个数字

5
我想知道在Makefile中添加两个数字的更好解决方案。在我的情况下,我将使用如下所示的add函数:
result = $(call add, 34, 56)
$(error $(result))                        

解决方案1:

add    = $(shell echo $$(( $(1) + $(2) )))

解决方案2:
add    = $(shell perl -e 'print $1 + $2')

解决方案3:

add    = $(shell echo '$1 + $2' | bc | tr '\n' ' ')

解决方案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) +))) 

我承认解决方案4看起来很荒谬,但它是唯一一个不依赖外部工具(例如bashperlbc)的方法。


1
算术展开是一种POSIX shell特性,因此您不必特别依赖于bash来解决方案1,任何sh都可以使用。 - Virgile
请看:https://dev59.com/wXI-5IYBdhLWcg3wTWVu#52715839 - Vroomfondel
1个回答

3

这个库太棒了! - nowox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接