makefile 中的条件错误

Conditional error in makefile

我的 MakeFile 中有以下命令:

.PHONY: lint
lint:
    eslint src --ext ts

.PHONY: component
component:
ifndef name
$(error name is not set. try 'make component name=')
endif

问题是当我 运行 make lint 或任何其他命令退出时出现 component.

中的错误

我该如何阻止这种情况发生?

Make 扩展 ifndef … 块作为解析 makefile 的一部分。它可以扩展到目标或命令。你写了“如果 name 没有定义,产生一个错误”所以 Make 会按照你的指示去做。

您似乎打算为 component 目标编写规则。如果是这样,您需要将解析时错误更改为 运行 时错误。

component:
ifdef name
        command_to_generate_component $(name)
else
        false "Error: name is not set. Try 'make component name=…'"
endif