我想知道哪种解决方案更好地在 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 很荒谬,但它是唯一一个不依赖于诸如
bash
之类的外部工具的解决方案。 , perl
或 bc
.
最佳答案
试试 GNU Make Standard Library ,它提供整数算术函数。
关于makefile - 如何在 Makefile 中正确添加两个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28981099/