variables - 生成文件 : how to create global variable available to sub-makefile?

标签 variables makefile gnu-make

给定一个 master.makefile调用子makefile,例如:

downloads:
    make -f downloads.makefile

还有一个子makefile downloads.makefile这样的 :
download: clean
    curl -o ./data/<itemname>.png 'http://www.mysite.com/<itemname>.png'
    echo "downloaded <itemname>: Done!"

如何设置全局变量 itemname在我的主文件中,可用于我的子制作文件?

注意:我的项目实际上更复杂,有 12 个不同的子 makefile,它们应该重用来自主文件的相同参数/变量。 masterfile 应该分配值,而 sub-makefile 应该检索我放置 <itemname> 的变量的值。 .

最佳答案

有多种方法可以做到。

首先,始终,始终使用 $(MAKE)从不make运行子制作时。然后...

1. 您可以在递归命令行上传递值:

itemname = whatever
downloads:
        $(MAKE) -f downloads.makefile itemname=$(itemname)

2. 您可以在父 makefile 中导出变量:
export itemname = myvalue
downloads:
        $(MAKE) -f downloads.makefile

带有子makefile,例如:
download: 
    echo "downloaded $(itemname): Done!"

关于variables - 生成文件 : how to create global variable available to sub-makefile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22882202/

相关文章:

makefile - make & gmake 兼容 if else 语句

makefile - Makefile 中的链接模式规则

linux - 构建 glibc-2.25 错误 : FAIL: nptl/tst-cond17

python - python替换错误。 (属性错误 : 'tuple' object has no attribute 'replace' )

C 多个单行声明

JavaScript 变量保护

linux - 抄送: unrecognized option '-KPIC'

javascript - 获取数组 Angular 的单个属性

makefile - CMAKE:构建库并链接到它

makefile - 使隐式规则在没有命令的情况下不起作用