转自:
多层级的makefile编写——递归调用makefile - wangyuling1234567890的专栏 - 博客频道 - CSDN.NET
http://blog.csdn.net/wangyuling1234567890/article/details/22111271
文件层级结构:
│ Makefile
│ tmp
│
├─include
│ public.h
│
└─src
├─moda
│ Makefile
│ moda.c
│ moda.h
│
└─modb
Makefile
modb.c
modb.h
public.h
moda.c (modb.c类似)
- #include <stdio.h>
- #include "moda.h"
- #include "public.h"
-
- int main(void)
- {
- printf("This is mod A, MAX_SIZE:%d \n", MAX_SIZE);
- return 0;
- }
顶层makefile:
- CC = gcc
-
- ROOT := $(shell pwd)
- INCLUDE := $(ROOT)/include
- SRC := $(ROOT)/src
-
- USR_SUB_DIR := $(SRC)/moda $(SRC)/modb
-
- default:usr
-
- usr:
- @for n in $(USR_SUB_DIR); do $(MAKE) -C $$n ; done
-
- clean:
- @for n in $(USR_SUB_DIR); do $(MAKE) -C $$n clean; done
moda目录下makefile:
- CC = gcc
-
- ROOT := $(shell pwd)/../..
- INCLUDE := $(ROOT)/include
-
- CFLAGS += -I$(INCLUDE)
-
- target:moda
-
- modb:moda.o
- $(CC) -o moda moda.o
-
- modb.o:moda.c
- $(CC) $(CFLAGS) -c moda.c
-
- clean:
- -rm *.o moda
执行:
- linux:/mnt/hgfs/vmware-share/makefile # <strong><span style="color:#000099;">make </span></strong>
- make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/moda'
- gcc -I/mnt/hgfs/vmware-share/makefile/src/moda/../../include -c -o moda.o moda.c
- gcc moda.o -o moda
- make[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/moda'
- make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/modb'
- gcc -I/mnt/hgfs/vmware-share/makefile/src/modb/../../include -c modb.c
- gcc -o modb modb.o
- make[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/modb'
- linux:/mnt/hgfs/vmware-share/makefile #
- linux:/mnt/hgfs/vmware-share/makefile # <strong><span style="color:#000099;">cd src/moda/</span></strong>
- linux:/mnt/hgfs/vmware-share/makefile/src/moda # <strong><span style="color:#000099;">ll</span></strong>
- 总用量 5
- drwxrwxrwx 1 root root 0 2014-03-25 23:51 .
- drwxrwxrwx 1 root root 0 2014-03-25 23:10 ..
- -rwxrwxrwx 1 root root 219 2014-03-25 23:30 Makefile
- -rwxrwxrwx 1 root root 6803 2014-03-25 23:51 moda
- -rwxrwxrwx 1 root root 149 2014-03-25 23:39 moda.c
- -rwxrwxrwx 1 root root 0 2014-03-25 23:10 moda.h
- -rwxrwxrwx 1 root root 908 2014-03-25 23:51 moda.o
- linux:/mnt/hgfs/vmware-share/makefile/src/moda # <strong><span style="color:#000099;">./moda</span></strong>
- This is mod A, MAX_SIZE:10
- <pre code_snippet_id="256920" snippet_file_name="blog_20140325_5_2131557" name="code" class="cpp">linux:/mnt/hgfs/vmware-share/makefile #
- linux:/mnt/hgfs/vmware-share/makefile #
- linux:/mnt/hgfs/vmware-share/makefile #</pre>linux:/mnt/hgfs/vmware-share/makefile # <strong><span style="color:#000099">make clean</span></strong><br>
- make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/moda'<br>
- rm *.o moda<br>
- make[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/moda'<br>
- make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/modb'<br>
- rm *.o modb<br>
- make[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/modb'<br>