在编译前在 makefile 回显 "compiling"

in makefile echoing "compiling" before the compilation

我认为这很容易,但我实际上无法弄清楚也无法在互联网上找到广告“对象文件的编译”(例如)的解决方案在编译之前,同时避免重新链接

我的 makefile 应该是这样的:

    libfrog.a: $(OBJS)
        @echo "building the library"
        ar -rc $@ $^
    %.o:%.c
        gcc -i. -c -o $@ $<

产生,当提示 "make" :

    gcc -I. -c -o file01.o file01.c
    gcc -I. -c -o file02.o file02.c
    gcc -I. -c -o file03.o file03.c
    gcc -I. -c -o file04.o file04.c
    gcc -I. -c -o file05.o file05.c
    building library
    ar -rc libfrog.a file01.o file02.o file03.o file04.o file05.o

但我想要:

    compilation of the objects files
    gcc -I. -c -o file01.o file01.c
    gcc -I. -c -o file02.o file02.c
    gcc -I. -c -o file03.o file03.c
    gcc -I. -c -o file04.o file04.c
    gcc -I. -c -o file05.o file05.c
    building library
    ar -rc libfrog.a file01.o file02.o file03.o file04.o file05.o

现在:

1 - 如果我在调用规则 libfrog.a 之前放置一个回显(比如创建另一个第一条规则),即使我提示 "make" 两次并且没有任何内容,它也会打印完成...

2 - 如果我在规则中添加回显 %.o:%.c 它将为每个文件打印

3 - 如果我将 echo 作为第一条规则的依赖项,它将在再次提示 "make" 时强制重新链接所有文件,即使只修改了一个文件 libfrog.a: echo $(OBJS) (和一个规则 "echo" 回应文本)...

所以我尝试对每个编译进行回显,但是将文本更改为不回显,我失败了...我尝试创建一个 echo.txt 文件只是为了创建一个依赖于它的规则存在,但我也失败了。我不知道这是否可能?

做起来并不简单。但是,如果您使用的是 GNU make,则可以在 order-only prerequisites:

的帮助下完成
%.o : %.c | announce
        gcc -I. -c -o $@ $<

.PHONY: announce
announce:
        echo "compilation of the objects files"

我个人认为不需要显示此消息,我什至不会在我的 makefile 中添加上述数量的复杂性来支持它。

但是如果你真的想要这样做并且你不希望打印消息除非需要制作一些目标文件,你必须真正喜欢并做这样的事情:

PRINTED :=

%.o : %.c
        $(or $(PRINTED),$(eval PRINTED := :)echo "compilation of the objects files")
        gcc -I. -c -o $@ $<     

一旦生效,您需要在第一个配方行之前添加 @