通配符 % 在 makefile 规则中如何工作
我有一个生成文件。
我需要从 src1 和 src2 下的源文件在 C:/bin/ 下构建一组目标。
顺序要求
${DEST_DIR}/ao ${DEST_DIR}/bo ${DEST_DIR}/co ${DEST_DIR}/do 的
让它们按照makefile 的某些部分
DEST_DIR = C:/bin
SRC_DIR1 = C:/src1
SRC_DIR2 = C:/src2
并且我对它们的规则如下
#rule#1
${DEST_DIR}/%.o : ${SRC_DIR1}/%.c
#the command required to build the target
echo "Source: $< Target: $@ "
#rule#2
${DEST_DIR}/%.o : $ {SRC_DIR2}/%.c
#the command required to build the target
echo "Source: $< Target: $@ "
让 ac 和 bc 位于 SRC_DIR1 中,cc 和 dc 位于 SRC_DIR2 中
当我构建它时,可以很好地构建 ao 和 bo 的规则#1,然后是 co 的规则#2,
我没想到就可以了。因为我期望它适用于 co 的规则#1,并抛出一个错误,指出“没有规则可以生成目标抄送”。但它适用于规则#2,并从 SRC_DIR2 中的 cc 构建 co。
有人可以解释一下它是如何工作的吗?在这种情况下,通配符 %
是如何工作的以及规则匹配是如何发生的?
I have a makefile.
I need to build a set of targets under C:/bin/ from the source files under src1 and src2.
let them be required in the order say
${DEST_DIR}/a.o ${DEST_DIR}/b.o ${DEST_DIR}/c.o ${DEST_DIR}/d.o
in some part of makefile
DEST_DIR = C:/bin
SRC_DIR1 = C:/src1
SRC_DIR2 = C:/src2
and i've the rules for them as follows
#rule#1
${DEST_DIR}/%.o : ${SRC_DIR1}/%.c
#the command required to build the target
echo "Source: lt; Target: $@ "
#rule#2
${DEST_DIR}/%.o : ${SRC_DIR2}/%.c
#the command required to build the target
echo "Source: lt; Target: $@ "
Let a.c and b.c are in SRC_DIR1 and c.c and d.c are in SRC_DIR2
When I build it builds fine going to rule#1 for a.o and b.o, then rule#2 for c.o and d.o
I didn't expect it'd be fine. Because I was expecting it to go for rule#1 for c.o and throw an error saying "no rule to make target c.c". But it goes for rule#2 and builds c.o from c.c in SRC_DIR2.
Can someone explain me how it works? How does the wild card character %
work in this case and how rule matching occurs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Make 不允许有两组命令,例如 ao,但它允许重叠的模式规则,即使目标模式完全相同。
当您请求 co 时,由于“co”没有特定的规则,因此 make 会查找模式规则以查找匹配项。来自 GNU make 手册:“为了要应用的模式规则,其目标模式必须与所考虑的文件名匹配,并且其所有先决条件(模式替换后)必须命名存在或可以创建的文件。”因此,第一条规则不适用,请继续执行第二条规则,第二条规则适用。
Make will not allow two sets of commands for, say, a.o, but it will allow overlapping pattern rules even if the target pattern is exactly the same.
When you ask for c.o, since there is no specific rule for "c.o", make looks through the pattern rules for a match. From the GNU make manual: "In order for the pattern rule to apply, its target pattern must match the file name under consideration and all of its prerequisites (after pattern substitution) must name files that exist or can be made." So the first rule doesn't apply, and make moves on to the second, which does.