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

时间:2015-03-11 07:47:39

标签: makefile

我想知道哪个解决方案最好在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 个答案:

答案 0 :(得分:2)

尝试GNU Make Standard Library,它提供整数运算函数。