makefile自定义函数
我试图在 Makefile 中创建一个自定义函数来检测当前平台并相应地返回正确的文件。这是我的尝试。
UNAME := $(shell uname -s)
define platform
ifeq ($(UNAME),Linux)
$1
else ifneq ($(findstring MINGW32_NT, $(UNAME)),)
$2
else ifeq ($(UNAME),Darwin)
$3
endif
endef
all:
@echo $(call platform,linux,windows,mac)
它失败并出现以下错误。
/bin/sh: Syntax error: "(" unexpected
[Finished]make: *** [all] Error 2
我做错了什么?
I'm trying to make a custom function in a Makefile to detect the current platform and return the proper file accordingly. Here is my attempt.
UNAME := $(shell uname -s)
define platform
ifeq ($(UNAME),Linux)
$1
else ifneq ($(findstring MINGW32_NT, $(UNAME)),)
$2
else ifeq ($(UNAME),Darwin)
$3
endif
endef
all:
@echo $(call platform,linux,windows,mac)
It fails with the following error.
/bin/sh: Syntax error: "(" unexpected
[Finished]make: *** [all] Error 2
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ifeq ... else ... endif
是我会将条件移出
define
指令。无论如何,在Make执行过程中目标平台是不会改变的,所以不需要每次调用platform
时都解析$(UNAME)
。ifeq ... else ... endif
are conditional directives in GNU Make, they can't appear insidedefine ... endef
because the latter treats them as a literal text. (Try to remove@
sign nearecho
command and you will see the actual result of evaluatingplatform
function)I'd move conditionals out of the
define
directive. Anyway, the target platform can't change during the execution of Make, so there is no need to parse$(UNAME)
each time you callplatform
.另一种选择是连接
uname
的输出以形成特定格式的平台字符串,并相应地命名特定于平台的 makefile:Another option would be to concatenate outputs of
uname
to form a platform string in a certain format and have platform specific makefiles named accordingly: