【发布时间】:2021-04-14 10:53:36
【问题描述】:
只是尝试使用这个骨架编写一个条件 Makefile:
TARGET = test
ifeq ($(FOO),y)
$(TARGET):
@echo This is test
$(TARGET)-a:
@echo This is test-a
$(TARGET)-b:
@echo This is test-b
else
$(info FOO is disabled)
endif
当 FOO 条件为真时,基于 TARGET 变量的规则集(由一个 $(TARGET) 和一组 $(TARGET)-substring 组成)按预期工作:
$ make test
This is test
$ make test-a
This is test-a
当 FOO 条件为假时,我想为我的所有目标定义一个默认规则,只是为了在屏幕上报告 FOO 变量被禁用。我不知道这样做的正确方法。尝试了一些选项:
选项1,使用骨架示例,始终打印字符串“FOO is disabled”,但会产生错误:
$ make test-a
FOO is disabled
make: *** No rule to make target 'test-a'. Stop.
$ make test
FOO is disabled
make: *** No rule to make target 'test'. Stop.
方案二,如果尝试这样修改错误规则:
else
$(TARGET)-%:
$(info FOO is disabled)
endif
然后所有 $(TARGET)-substring 目标按预期工作:
$ make test-a
FOO is disabled
make: 'test-a' is up to date.
$ make test-b
FOO is disabled
make: 'test-b' is up to date.
但是这个规则在生成 $(TARGET) 时失败了:
$ make test
make: *** No rule to make target 'test'. Stop.
选项3,如果尝试删除选项2中定义的错误规则上的连字符:
else
$(TARGET)%:
$(info FOO is disabled)
endif
然后让 $(TARGET) 执行编译 test.o 目标文件的默认规则:
$ make test
FOO is disabled
cc test.o -o test
cc: error: test.o: No such file or directory
cc: fatal error: no input files
compilation terminated.
make: *** [<builtin>: test] Error 1
我变得有点疯狂,试图满足这个默认规则。请对此提供一些帮助,这将非常有用。咳咳!
【问题讨论】:
标签: makefile default target rules