使 ifeq 错误: 语法错误接近意外令牌

我正在编写一个 Makefile,它在一个地方进行字符串匹配,代码是这样的:

if test ...; \
then \
shell scripts... \
fi


ifeq ($(DIST_TYPE),nightly)
shell scripts ...
endif

这里的第一个 if是 shell 脚本,第二个 ifeq是 GNU Make 的条件:

Ifeq (夜间,夜间)

/bin/sh:-c: 第0行: 意外标记附近的语法错误‘ night,nnight’

/bin/sh:-c: 第0行: ‘ ifeq (夜间,夜间)’

这里发生了什么事? 看起来马克正试图呼叫外壳。

38941 次浏览

I played around the code and found that the conditional statements should be written without indentation, and this solved my problem.

If there is no indentation, Make will treat it as a directive for itself; otherwise, it's regarded as a shell script.

Example code

Wrong:

target:
ifeq (foo, bar)
...
endif

Correct:

target:
ifeq (foo, bar)
...
endif

In addition, if the conditional statements is used in define functions, like:

define myFunc
ifeq (foo, bar)
...
endif
endef

In this case, Make will also treat it as a shell script.

This problem can be solved by using if-function instead:

define myFunc
$(if condition,then-part[,else-part])
endef