【问题标题】:Define in Makefile在 Makefile 中定义
【发布时间】:2015-08-15 17:14:52
【问题描述】:

我有这样的文件和目录结构:

.
├── Makefile
└── packages
    ├── Makefile
    └── subdir
        └── Makefile

顶部的 Makefile 看起来像这样:

define aa
    make -C $1 $2
endef

packages=$(shell find ./packages -type d)
p1=$(filter-out . .., $(packages))

all:
    $(foreach f,$(p1),$(call aa,$(f),compile))

./packages/ 和 ./packages/subdir/ 中的 Makefile 都有“编译”目标。

我正在尝试在“packages”子目录中自动调用所有 Makefile,而不是将它们单独添加到 Makefile 中。

当我在顶级目录中运行 make 时出现错误:

make -C ./packages compile  make -C ./packages/subdir compile
make[1]: *** packages/subdir: No such file or directory.  Stop.
Makefile:16: recipe for target 'all' failed
make: *** [all] Error 2

我想知道为什么 make 的两个调用(应该是单独的调用)都放在一行中?

当我像这样在“aa”宏的末尾添加行尾时:

define aa
    make -C $1 $2

endef

一切都按预期进行。 我的问题是为什么没有这个行尾这个宏就不能工作?

【问题讨论】:

    标签: makefile


    【解决方案1】:

    因为变量定义在define 之后的换行符之后开始,并在endef 之前的换行符之前结束。

    引用 gnumake 手册:

    普通赋值中的值不能包含换行符;但是在定义中分隔值行的换行符成为变量值的一部分(除了在 endef 之前的最后一个换行符并且不被视为值的一部分)。

    所以你的定义等同于aa = make -C $1 $2,因为 foreach 不会在每次扩展之间添加任何内容,所以你得到了这个结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 2014-02-15
      • 1970-01-01
      相关资源
      最近更新 更多