【问题标题】:`make depends` magic in gnu make?`make 依赖`gnu make 中的魔法?
【发布时间】:2015-05-09 07:12:26
【问题描述】:

我有一个生成依赖文件的简单脚本 (depends.sh),并对依赖文件进行了一些更改。

#!/bin/sh
#echo "## Got: $*"
CC="$1"
DIR="$2"
shift 2
case "$DIR" in
    "" | ".")
    $CC -MM -MG "$@" | sed -e 's@^\(.*\)\.o:@\1.d \1.o:@'
    ;;
    *)
    $CC -MM -MG "$@" | sed -e "s@^\(.*\)\.o:@$DIR/\1.d $DIR/\1.o:@"
    ;;
esac

脚本是从 Makefile 调用的,这是它的摘录。

DEP := $(OBJ:.o=.d)

# implicit rules

%.d: %.c
    ./depends.sh $(CC) `dirname $*.c` $(CFLAGS) $*.c > $@

-include $(DEP)

# Actual targets

depend: $(DEP)

有趣的是make depends做了以下动作:

./depends.sh gcc `dirname src/hellomake.c` -Wall -Wno-unused-function -g -O  -Isrc  src/hellomake.c > src/hellomake.d
./depends.sh gcc `dirname src/hellofunc.c` -Wall -Wno-unused-function -g -O  -Isrc  src/hellofunc.c > src/hellofunc.d
cat depends.sh >depends 
chmod a+x depends

没有depends 目标(只有depend 目标),但它执行依赖目标,甚至创建依赖脚本,并使其可执行。

这背后有什么魔力?

【问题讨论】:

    标签: makefile gnu-make


    【解决方案1】:

    最后两行来自一个内置规则,如果您使用了 make 的“-p”选项,您可以看到它。看起来像

    %.sh:
    
    %: %.sh
    #  commands to execute (built-in):
        cat $< >$@
        chmod a+x $@
    

    它触发是因为您的脚本名为“depends.sh”。运行另外两行是因为 make 需要 $(DEPS) 数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多