makefile target dependencies dependent on target name

烈酒焚心 提交于 2019-12-24 08:46:54

问题


I have the following rule:

SPECIAL = file1 file2

%.o : %.cpp a.h
    $(CC) -c $(CFLAGS) $< -o $@

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?


回答1:


You can assign additional dependencies separately. Just add a line at the end:

$(addsuffix .o,${SPECIAL}): b.h

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   


来源:https://stackoverflow.com/questions/44415983/makefile-target-dependencies-dependent-on-target-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!