如何在makefile中使用数值变量?

时间:2018-04-01 10:33:57

标签: makefile dd

我尝试连接一系列文件,填充零,以便每个文件占用512字节的倍数,并使用dd形成一张软盘图像。

我想象makefile看起来像这样:

CurrentLocation := 1
build:
    dd if=bootloader of=result bs=1474560 count=1 conv=sync # writes sector 0, guaranteed to be 512B
    dd if=file1 of=result obs=512 conv=sync,notrunc # writes file1 
    # get file size using 'stat' command and convert to sectors
    dd if=file2 of=result obs=512 conv=sync,notrunc seek=#CurrentLocation+filesize
    ... and so on ...

但我无法弄清楚如何做到(或者有更好的方法)。

1 个答案:

答案 0 :(得分:1)

我不确定你打算做什么数字。但是,我认为这个例子将为您提供所需的信息:

SHELL = /usr/bin/env bash

CurrentLocation := 1
build:
        ((sum = $(CurrentLocation) + 2)); echo $$sum

这里我使用了((...)) bash语法,它可以处理整数运算。另请注意我如何以不同方式转义Make变量CurrentLocation和bash变量sum。另请注意,我在同一行中定义并引用sum。这很重要,因为Make在不同的子shell中运行每一行,这意味着在下一行没有定义sum。最后,我发现我必须明确告诉Make使用例如使用bash shell。 SHELL = /usr/bin/env bash

请注意,您无法使用此技术更新Make变量CurrentLocation