Makefile目标生成失败:目标模式不匹配问题咨询
Let's break down why you're hitting this error and how to fix it quickly:
The Root Cause
GNU Make doesn't support combining a variable list of targets (like $(LOCAL_DEPENDS)) with a pattern rule (the .%.d:%.cpp part). When you write $(LOCAL_DEPENDS):.%.d:%.cpp, Make treats $(LOCAL_DEPENDS) as a list of explicit targets, then tries to match each against the .%.d pattern—and since your targets look like ./utils/.StrUtils.d, they don't align with the pattern in the way you expect, hence the mismatch error.
When you remove the $(LOCAL_DEPENDS): prefix and just write .%.d:%.cpp, you're using a standard pattern rule: this tells Make "for any .cpp file, here's how to generate the corresponding .%.d dependency file"—no need to pre-list all targets, Make handles the matching automatically.
The Fix
Modify your dependency-generation rules to remove the variable prefixes. Here's the corrected code snippet for those sections:
# Generate .d files from .cpp sources (no target list prefix) .%.d:%.cpp @set -e; rm -f $@;\ $(CXX) -MM $(CFLAGS) $(LOCAL_INC) $< > $@.$$$$;\ sed 's,\($*\)\.o[:]*,$@:,g' < $@.$$$$ > $@;\ rm $@.$$$$ # Generate .d files from .hpp headers (no target list prefix) .%.d:%.hpp @set -e; rm -f $@;\ $(CXX) -MM $(CFLAGS) $(LOCAL_INC) $< > $@.$$$$;\ sed 's,\($*\)\.o[:]*,$@:,g' < $@.$$$$ > $@;\ rm $@.$$$$
Keep your existing -include $(LOCAL_DEPENDS) and -include $(LOCAL_HPP) lines—those still tell Make to load the generated dependency files once they're created.
Optional Cleanup Tip
Your LOCAL_DEPENDS variable definition can be simplified to avoid the foreach loop, making it more readable and less error-prone:
# Replace your current LOCAL_DEPENDS lines with this LOCAL_DEPENDS := $(foreach src,$(LOCAL_SRC),$(dir $(src)).$(notdir $(src:.cpp=.d)))
This does the same path transformation (turning ./utils/StrUtils.cpp into ./utils/.StrUtils.d) but in a more concise, Make-idiomatic way.
内容的提问来源于stack exchange,提问作者trueno

