首页 文章

wild字符%如何在makefile规则中起作用

提问于
浏览
0

我有一个makefile .

我需要在src1和src2下的源文件下在C:/ bin /下构建一组目标 .

让他们按顺序说出来

$ /a.o $ /b.o $ /c.o $ /d.o

在makefile的某些部分

DEST_DIR = C:/ bin

SRC_DIR1 = C:/ src1

SRC_DIR2 = C:/ src2

我的规则如下

#规则1

$ /%.o:$ /% . c

#the command required to build the target
  echo "Source: $< Target: $@ "

#规则#2

$ /%.o:$ /% . c

#the command required to build the target
  echo "Source: $< Target: $@ "

设a.c和b.c在SRC_DIR1中,c.c和d.c在SRC_DIR2中

当我构建它时,为a.o和b.o构建精细的规则#1,然后为c.o和d.o规则#2

我没想到会没事的 . 因为我期望它为c.o的规则#1而且抛出一个错误,说“没有规则来制作目标c.c” . 但它适用于规则#2并在SRC_DIR2中从c.c构建c.o.

谁能解释一下它是如何工作的?在这种情况下,通配符 % 如何工作以及如何进行规则匹配?

1 回答

  • 1

    Make将不允许使用两组命令,例如a.o,但即使目标模式完全相同,它也将允许重叠模式规则 .

    当您要求c.o时,由于"c.o"没有特定规则,因此请查看匹配的模式规则 . 从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."所以第一条规则不适用,并继续前进到第二条规则 .

相关问题