【问题标题】:epoch difference in MakefilesMakefile 的时代差异
【发布时间】:2014-07-11 15:09:18
【问题描述】:

以下是我的 Makefile 的一部分(有错误)。我只想要 epoch 和 epoch max 之间的差异并打印它们。但我不确定错误是什么。有人可以帮我解决这个问题。 谢谢

EPOCH_MAX = 1400000000

identifier:
    epoch = $(shell date +%s)
    echo $(epoch)
    residue = $(epoch)-$(EPOCH_MAX) 
    echo $(residue)

我收到以下错误

epoch = 1400767572
make: epoch: Command not found
make: *** [identifier] Error 127

【问题讨论】:

    标签: bash makefile epoch gnu-make


    【解决方案1】:

    以下似乎也可以正常工作。但是使用shell。

    EPOCH_MAX = 1400000000
    epoch = $(shell date +%s )
    residue =$(shell echo $(epoch)\-$(EPOCH_MAX) | bc)
    
    
    identifier:
       echo $(epoch)
       echo $(residue)
    

    【讨论】:

      【解决方案2】:

      您正在混淆 Makefile 变量和 shell 变量。 以下代码可能是您想要的:

        EPOCH_MAX = 1400000000
        epoch = $(shell date +%s )
      
        identifier:
              echo $(epoch)
              (( residue = $(epoch) - $(EPOCH_MAX) )) || true ;\
              echo $${residue}
      

      编辑: 正如 MadScientist 所强调的,这根本不是便携式的。更好的是例如:

        EPOCH_MAX = 1400000000
      
        identifier:
            epoch=$(shell date +%s ); \
            echo $$epoch; \
            residue=`expr $$epoch - $(EPOCH_MAX)`; \
            echo $$residue
      

      【讨论】:

      • 不要在 make recipes 中使用 bash 结构。它不是便携式的。仅使用 POSIX shell 结构。
      • 这里也不需要|| true,因为echo是最后一个命令,它的退出码是0;你不在乎分配的退出代码是什么。
      • 谢谢,我是 Makefiles 新手。但仍然没有打印“剩余”值。它说 /bin/sh: 1: 残留物:未找到
      • @user2532296:第 6 行末尾有空格吗?行必须以反斜杠结尾。
      • 我没有空间了。我收到以下错误。 echo 1400770606 1400770606 ((residue = 1400770606 - 1400000000 ));\ echo ${residue} /bin/sh: 1: 残基:未找到
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多