在makefile中包含define指令中的文件

时间:2010-10-19 11:26:19

标签: makefile

我有一个makefile,definitions.mk,包含以下内容:

ifneq ($(MY_VARIABLE),true)
define my-function
  < some code defining the function)
endef
else
define my-function
 < different code defining the function>
endef
endif

现在,如果MY_VARIABLE设置为false,我想要包含另一个包含my-function定义的文件。像:

ifneq ($(MY_VARIABLE),true)
define my-function
  < some code defining the function)
endef
else
 <include file containing definition of function>
endif

问题在于我无法弄清楚如何做到这一点。如果我写:

include $(BUILD_SYSTEM)/my_function.mk

我会收到错误说:

/bin/bash/: include: command not found

那么,我该怎么做呢?

感谢任何输入!

1 个答案:

答案 0 :(得分:4)

实际上,您的第二个示例应该可以正常工作。根据您看到的错误,我想尽管您已经在此处正确输入了代码,但实际你的makefile中有这样的东西:

ifneq ($(MY_VARIABLE),true)
define my-function
    thing1
    thing2
endef
else
define my-function
    include $(BUILD_SYSTEM)/my_function.mk
endef
endif

也就是说,你有include指令 define指令中。由于define的工作方式,这不起作用。请记住,define只是语法糖,可以更容易地创建具有多行值的变量。无论如何,这意味着:

define FOO
    something
endef

与此相同:

FOO=something

我怀疑你做的有效是my-function=include my_function.mk。然后,当您尝试引用my-function时,它会扩展为include my_function.mk,然后传递给shell,当然,这一点无法理解。

非常清楚,以下是如何使这项工作。首先,请确保my_function.mk中有define / endef

define my-function
    red fish
    blue fish
endef

然后在definitions.mk中你需要这个:

ifneq ($(MY_VARIABLE),true)
define my-function
    thing1
    thing2
endef
else
    include $(BUILD_SYSTEM)/my_function.mk
endif

请注意define行周围没有endef / include行!