如何在 Makefile 中正确地添加两个数字
How to properly add two numbers in a Makefile
我想知道在 Makefile 中添加两个数字哪种解决方案更好。在我的例子中,我将使用函数 add
,如下所示:
result = $(call add, 34, 56)
$(error $(result))
解决方案 1:
add = $(shell echo $$(( $(1) + $(2) )))
解决方案 2:
add = $(shell perl -e 'print + ')
解决方案 3:
add = $(shell echo ' + ' | 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 )
_encode = $(wordlist 1,,$(_input_int))
_plus =
_max = $(subst xx,x,$(join ,))
_push = $(eval stack := $ $(stack))
_pop = $(word 1,$(stack))$(eval stack := $(wordlist 2,$(words $(stack)),$(stack)))
_pope = $(call _encode,$(call _pop))
_pushd = $(call _push,$(call _decode,))
calculate=$(eval stack:=)$(foreach t,,$(call handle,$t))$(stack)
handle =$(call _pushd, \
$(if $(filter +,), \
$(call _plus,$(call _pope),$(call _pope)), \
$(call _encode,)))
add = $(strip $(foreach v,$(2), $(call calculate, $v $(1) +)))
我承认解决方案 4 很荒谬,但它是唯一一个不依赖外部工具的解决方案,例如 bash
、perl
或 bc
。
试试GNU Make Standard Library,它提供整数运算功能。
我想知道在 Makefile 中添加两个数字哪种解决方案更好。在我的例子中,我将使用函数 add
,如下所示:
result = $(call add, 34, 56)
$(error $(result))
解决方案 1:
add = $(shell echo $$(( $(1) + $(2) )))
解决方案 2:
add = $(shell perl -e 'print + ')
解决方案 3:
add = $(shell echo ' + ' | 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 )
_encode = $(wordlist 1,,$(_input_int))
_plus =
_max = $(subst xx,x,$(join ,))
_push = $(eval stack := $ $(stack))
_pop = $(word 1,$(stack))$(eval stack := $(wordlist 2,$(words $(stack)),$(stack)))
_pope = $(call _encode,$(call _pop))
_pushd = $(call _push,$(call _decode,))
calculate=$(eval stack:=)$(foreach t,,$(call handle,$t))$(stack)
handle =$(call _pushd, \
$(if $(filter +,), \
$(call _plus,$(call _pope),$(call _pope)), \
$(call _encode,)))
add = $(strip $(foreach v,$(2), $(call calculate, $v $(1) +)))
我承认解决方案 4 很荒谬,但它是唯一一个不依赖外部工具的解决方案,例如 bash
、perl
或 bc
。
试试GNU Make Standard Library,它提供整数运算功能。