Makefile目标依赖关系取决于目标名称 [英] makefile target dependencies dependent on target name
问题描述
我有以下规则:
SPECIAL = file1 file2
%.o : %.cpp a.h
$(CC) -c $(CFLAGS) $< -o $@
我想如果%
在$(SPECIAL)
中,则将b.h
添加到依赖项列表中.
I would like that if %
is in $(SPECIAL)
, then b.h
is added to the list of dependencies.
有没有一种方法,而无需重复规则?
Is there a way to do it, without repeating the rule?
推荐答案
您可以单独分配其他依赖项.只需在最后添加一行:
You can assign additional dependencies separately. Just add a line at the end:
$(addsuffix .o,${SPECIAL}): b.h
不必处理依赖关系顺序,可以将规则中的$<
替换为$(filter %.cpp,$^)
.这样%.cpp
不必是第一个依赖项.
To not have to deal with dependency order, replace $<
in the rule with $(filter %.cpp,$^)
. This way %.cpp
does not have to be the first dependency.
理想情况下,您希望自动生成标头依赖项,以避免手动指定它们.
Ideally, you want the header dependencies to be generated automatically to avoid specifying them manually.
最简单的自动依赖项生成:
The most simple automatic dependency generation:
%.o : %.cpp
$(CXX) -c -o $@ -MD -MP $(CXXFLAGS) $(filter %.cpp,$^)
ifneq ($(MAKECMDGOALS),clean)
-include $(wildcard *.d)
endif
这篇关于Makefile目标依赖关系取决于目标名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!