如何在makefile中进行算术运算?

时间:2011-07-06 08:36:19

标签: shell makefile

我在makefile中执行以下操作来测量执行某些操作所需的时间: -

START=$(shell date +%s) <br>
@if [ -s mfill.mapi.diff ]; then echo "difference exist between GOLDEN map file and test map file, see mfill.map.diff" ; fi <br>
END=$(shell date +%s) <br>
DIFF_SUB=$(shell echo $(END)\-$(START) | bc) <br>
@echo "It took ${DIFF_SUB} seconds"

导致以下输出: -

  

START = 1309941257
  END = 1309941268
  DIFF_SUB =
  花了几秒钟

你们可以建议我做错了吗?

1 个答案:

答案 0 :(得分:1)

我认为你不应该对Makefile变量使用花括号:$(END)而不是$ {END}

你想要减法,但你使用'*'。

jcomeau@intrepid:/tmp$ cat Makefile 
START=3
END=5
DIFF_SUB=$(shell echo $(END)-$(START) | bc)
test:
    @echo it took $(DIFF_SUB) seconds
jcomeau@intrepid:/tmp$ make
it took 2 seconds

如果您在目标中执行这些操作,请使用花括号,但要加倍$ s:$${DIFF_SUB}

jcomeau@intrepid:/tmp$ cat Makefile 
START=3
END=5
DIFF_SUB=$(shell echo $(END)-$(START) | bc)
test:
    @echo it took $(DIFF_SUB) seconds
test2:
    @START=3 && \
    echo HELLO && \
    END=7 && \
    DIFF_SUB=$$(($$END - $$START)) && \
    echo it took $${DIFF_SUB} seconds
jcomeau@intrepid:/tmp$ make test test2
it took 2 seconds
HELLO
it took 4 seconds