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

标签 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 很荒谬,但它是唯一一个不依赖于诸如 bash 之类的外部工具的解决方案。 , perlbc .

最佳答案

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

关于makefile - 如何在 Makefile 中正确添加两个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28981099/

相关文章:

linux - 关于 skia 库的指南

python - 如何在外部脚本错误后强制 make 退出,但在编译错误后不退出

makefile - 让 make 在特定目录中创建目标文件

node.js - 在 Node 项目上运行 make 命令

makefile - 为不同目录下的源文件编写makefile

Makefile 依赖不会触发重新编译?

makefile - 制作 : Setting target-specific variables in static pattern rules

c - makefile(链接2个源文件,非主文件)

makefile - 如何执行列表中的每个命令?

Makefile:21: *** 配方在第一个目标之前开始。停止