59

有人可以解释如何在 Makefiles 中使用 if-then 语句和 for 循环吗?我似乎找不到任何带有示例的好的文档。

4

4 回答 4

67

条件形式

简单的

conditional-directive
text-if-true
endif

中度复杂

conditional-directive
text-if-true
else
text-if-false
endif

更复杂

conditional-directive
text-if-one-is-true
else
conditional-directive
text-if-true
else
text-if-false
endif
endif

条件指令

如果相等语法

ifeq (arg1, arg2)
ifeq 'arg1' 'arg2'
ifeq "arg1" "arg2"
ifeq "arg1" 'arg2'
ifeq 'arg1' "arg2"

如果不相等语法

ifneq (arg1, arg2)
ifneq 'arg1' 'arg2'
ifneq "arg1" "arg2"
ifneq "arg1" 'arg2'
ifneq 'arg1' "arg2"

如果定义语法

ifdef variable-name

如果未定义语法

ifndef variable-name  

foreach 函数

foreach 函数语法

$(foreach var, list, text)  

foreach 语义
对于“list”中的每个空格分隔的单词,将“var”命名的变量设置为该单词并执行文本。

于 2008-10-07T23:28:12.943 回答
19

这是一个示例,如果:

ifeq ($(strip $(OS)),Linux)
        PYTHON = /usr/bin/python
        FIND = /usr/bin/find
endif

请注意,这带有一个警告,即不同版本的 Make 语法略有不同,似乎没有一个很好地记录在案。

于 2008-10-07T23:02:19.487 回答
7

您是否尝试过GNU make 文档?它有一整节关于带示例的条件。

于 2008-10-07T22:59:57.697 回答
5

您确实经常看到 for 循环,但通常不需要它们。这是一个示例,说明如何在不借助 shell 的情况下执行 for 循环

LIST_OF_THINGS_TO_DO = do_this do_that 
$(LIST_OF_THINGS_TO_DO): 
       run $@ > $@.out

SUBDIRS = snafu fubar
$(SUBDIRS):
     cd $@ && $(MAKE)
于 2011-05-05T05:36:30.190 回答