有人可以解释如何在Makefile中使用if-then语句和for循环吗?我似乎无法通过示例找到任何好的文档.
Joh*_*der 61
有条件的表格
简单
conditional-directive
text-if-true
endif
Run Code Online (Sandbox Code Playgroud)
中等复杂
conditional-directive
text-if-true
else
text-if-false
endif
Run Code Online (Sandbox Code Playgroud)
更复杂
conditional-directive
text-if-one-is-true
else
conditional-directive
text-if-true
else
text-if-false
endif
endif
Run Code Online (Sandbox Code Playgroud)
条件指令
如果是等于句法
ifeq (arg1, arg2)
ifeq 'arg1' 'arg2'
ifeq "arg1" "arg2"
ifeq "arg1" 'arg2'
ifeq 'arg1' "arg2"
Run Code Online (Sandbox Code Playgroud)
如果不等于语法
ifneq (arg1, arg2)
ifneq 'arg1' 'arg2'
ifneq "arg1" "arg2"
ifneq "arg1" 'arg2'
ifneq 'arg1' "arg2"
Run Code Online (Sandbox Code Playgroud)
如果定义了语法
ifdef variable-name
Run Code Online (Sandbox Code Playgroud)
如果未定义语法
ifndef variable-name
Run Code Online (Sandbox Code Playgroud)
foreach功能
foreach函数语法
$(foreach var, list, text)
Run Code Online (Sandbox Code Playgroud)
foreach语义
对于"list"中每个空格分隔的单词,由"var"命名的变量设置为该单词并执行文本.
Mar*_*ddy 16
这是一个例子,如果:
ifeq ($(strip $(OS)),Linux)
PYTHON = /usr/bin/python
FIND = /usr/bin/find
endif
Run Code Online (Sandbox Code Playgroud)
请注意,这有一个警告,即不同版本的Make具有略微不同的语法,其中没有一个似乎记录得很好.
小智 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)
Run Code Online (Sandbox Code Playgroud)