Make recursive

Posted youchihwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Make recursive相关的知识,希望对你有一定的参考价值。

folder structure:

Makefile
t1/Makefile
t1/t1.c
t2/Makefile
t2/t2.c

Makefile

SUBDIRS = t1 t2
all:
    for dir in $(SUBDIRS); do         $(MAKE) -C $$dir;     done

t1/Makefile

all: t1

clean:
    rm t1

t1/t1.c

#include <stdio.h>
int main(void)
{
        printf("t1 
");
        return 0;
}

t2/Makefile

all: t2

clean:
    rm t2

t2/t2.c

#include <stdio.h>
int main(void)
{
        printf("t2 
");
        return 0;
}

===========================================

case 1:

如果我們將 t1/t1.c 故意寫錯
t1/t1.c

#include <stdio.h>
int main(void)
{
        printf("t1 
");
        xxxxxxxxxxxxxxxxxxxxxxxx
        return 0;
}

接下 make

$ make

for dir in t1 t2; do     make -C $dir; done
make[1]: Entering directory '/home/break-through/working_space/wasai/make_test/t1'
cc     t1.c   -o t1
t1.c: In function ‘main’:
t1.c:5:9: error: ‘asdfasd’ undeclared (first use in this function)
         asdfasd
         ^
t1.c:5:9: note: each undeclared identifier is reported only once for each function it appears in
t1.c:6:9: error: expected ‘;’ before ‘return’
         return 0;
         ^
<builtin>: recipe for target 't1' failed
make[1]: *** [t1] Error 1
make[1]: Leaving directory '/home/break-through/working_space/wasai/make_test/t1'
make[1]: Entering directory '/home/break-through/working_space/wasai/make_test/t2'
cc     t2.c   -o t2
make[1]: Leaving directory '/home/break-through/working_space/wasai/make_test/t2'

發生什麼事呢?
當 build 到 t1 時,會有error,
但是並沒有立即 停下來,而是繼續執行 build t2,
這個會造成無法判斷是否 build success,
且也不曉得 error 在那裡,
怎麼說呢?
假如要 build 的 code 很多,
第一隻 code build error,
沒有立即停下,
繼續 build code,
而 營幕 會被一直刷新而無法判斷是否 build successful,
也不曉得 error 在哪裡?

結論:

不要使用 for loop build code

=======================================

case2:

若我們將 Makefile 改成如下,

SUBDIRS = t1 t2
BUILDSUBDIRS = $(SUBDIRS:%=build-%)

all: $(BUILDSUBDIRS)

$(BUILDSUBDIRS):
    make -C $(@:build-%=%)

t1/t1.c 故意寫錯

#include <stdio.h>
int main(void)
{
        printf("t1 
");
        xxxxxxxxxxxxxxxxxxxxxxxx
        return 0;
}

執行 make

$ make

make -C t1
make[1]: Entering directory '/home/break-through/working_space/wasai/make_test/t1'
cc     t1.c   -o t1
t1.c: In function ‘main’:
t1.c:5:9: error: ‘asdfasd’ undeclared (first use in this function)
         asdfasd
         ^
t1.c:5:9: note: each undeclared identifier is reported only once for each function it appears in
t1.c:6:9: error: expected ‘;’ before ‘return’
         return 0;
         ^
<builtin>: recipe for target 't1' failed
make[1]: *** [t1] Error 1
make[1]: Leaving directory '/home/break-through/working_space/wasai/make_test/t1'
Makefile:7: recipe for target 'build-t1' failed
make: *** [build-t1] Error 2

看到了嗎?
一旦有 error,立即停止,
不會 build t2

以上是关于Make recursive的主要内容,如果未能解决你的问题,请参考以下文章

CentOS下安装mysql,make时出现make[1]: *** [link_sources] Error 1 make: *** [all-recursive] Error 1

did you register the component correctly? For recursive components, make sure to provide the "n

使用Vue自定义组件时,报did you register the component correctly? For recursive components, make sure to provid

httpd Server not started: (13)Permission denied: make_sock: could not bind to address [::]:8888(代码片段

[异常解决] Make nRF51 DFU Project Appear "fatal error: uECC.h: No such file or directory"(代码片段

1 代码片段1