Shell 在Makefile中进行简单的数学运算
我需要在Makefile中做一些简单的数学运算,但找不到如何做。在shell脚本中,我会这样做:Shell 在Makefile中进行简单的数学运算,shell,makefile,Shell,Makefile,我需要在Makefile中做一些简单的数学运算,但找不到如何做。在shell脚本中,我会这样做: A=1 B=2 C=$[$A+$B] 但是如何在Makefile中执行此操作?让我们以此为基础: A=1 B=2 C=$(A)+$(B) all: @echo 'A is $(A)' @echo 'B is $(B)' @echo 'C is $(C)' 自然地,这输出C是1+2。如何重写变量C的定义,使Makefile输出C为3?在shell中
A=1
B=2
C=$[$A+$B]
但是如何在Makefile中执行此操作?让我们以此为基础:
A=1
B=2
C=$(A)+$(B)
all:
@echo 'A is $(A)'
@echo 'B is $(B)'
@echo 'C is $(C)'
自然地,这输出C是1+2
。如何重写变量C
的定义,使Makefile输出C为3
?在shell中使用“$(())”
一年后编辑:
我不知道您需要在Makefile中使用$C的最终值。(您刚刚展示了它只在Makefile中的shell脚本中使用,或者我犯了错误)您只需要将我的代码移动到$C的定义:
C=$(shell echo $$(($A+$B)))
就像我说的
我发现以下两种方法都使用shell,效果很好:$$(…)和
expr…
A=1
B=2
#With Double-dollar
SUM_1 = $(shell echo $$(( $(A) + $(B) )))
#With 'expr'
SUM_2 = $(shell expr $(A) + $(B) )
$(info Sum with Double-$$: $(SUM_1))
$(info Sum with 'expr': $(SUM_2))
请注意,使用expr
时,您应在+
周围放置空格,否则它将返回1+2
。当使用$$
时,这不是必需的
当您有可用的bc
时,您应该使用它,因为它的代码更清晰:
A=1
B=2
SUM = $(shell echo $(A)+$(B) | bc )
$(info Sum with 'bc': $(SUM))
我发现下面的页面非常有趣:你就不能用shell吗?或者您需要目标中的计算值吗?不,您只是修复了输出。但是C仍然是字符串“1+2”。@kayahr:我也没有改变C。
A=1
B=2
#With Double-dollar
SUM_1 = $(shell echo $$(( $(A) + $(B) )))
#With 'expr'
SUM_2 = $(shell expr $(A) + $(B) )
$(info Sum with Double-$$: $(SUM_1))
$(info Sum with 'expr': $(SUM_2))
A=1
B=2
SUM = $(shell echo $(A)+$(B) | bc )
$(info Sum with 'bc': $(SUM))