【转】多层级的makefile编写——递归调用makefile

转自:

多层级的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

 

[cpp] view plain copy
 
  1. #define MAX_SIZE 10  
 


moda.c  (modb.c类似)

 

 

[cpp] view plain copy
 
  1. #include <stdio.h>  
  2. #include "moda.h"  
  3. #include "public.h"  
  4.   
  5. int main(void)  
  6. {  
  7.     printf("This is mod A, MAX_SIZE:%d \n", MAX_SIZE);  
  8.     return 0;  
  9. }  

 

 

顶层makefile:

 

[cpp] view plain copy
 
  1. CC = gcc  
  2.   
  3. ROOT := $(shell pwd)  
  4. INCLUDE := $(ROOT)/include  
  5. SRC := $(ROOT)/src  
  6.   
  7. USR_SUB_DIR := $(SRC)/moda $(SRC)/modb  
  8.   
  9. default:usr  
  10.   
  11. usr:  
  12.     @for n in $(USR_SUB_DIR); do $(MAKE) -C $$n ; done  
  13.       
  14. clean:  
  15.     @for n in $(USR_SUB_DIR); do $(MAKE) -C $$n clean; done  


moda目录下makefile

 

 

[cpp] view plain copy
 
  1. CC = gcc  
  2.   
  3. ROOT := $(shell pwd)/../..  
  4. INCLUDE := $(ROOT)/include  
  5.   
  6. CFLAGS += -I$(INCLUDE)  
  7.   
  8. target:moda  
  9.   
  10. modb:moda.o  
  11.     $(CC) -o moda moda.o  
  12.   
  13. modb.o:moda.c  
  14.     $(CC) $(CFLAGS) -c moda.c   
  15.       
  16. clean:  
  17.     -rm *.o moda  


执行:

 

 

[cpp] view plain copy
 
  1. linux:/mnt/hgfs/vmware-share/makefile # <strong><span style="color:#000099;">make </span></strong>  
  2. make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/moda'  
  3. gcc -I/mnt/hgfs/vmware-share/makefile/src/moda/../../include   -c -o moda.o moda.c  
  4. gcc   moda.o   -o moda  
  5. make[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/moda'  
  6. make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/modb'  
  7. gcc -I/mnt/hgfs/vmware-share/makefile/src/modb/../../include -c modb.c   
  8. gcc -o modb modb.o  
  9. make[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/modb'  
  10. linux:/mnt/hgfs/vmware-share/makefile #  
  11. linux:/mnt/hgfs/vmware-share/makefile # <strong><span style="color:#000099;">cd src/moda/</span></strong>  
  12. linux:/mnt/hgfs/vmware-share/makefile/src/moda # <strong><span style="color:#000099;">ll</span></strong>  
  13. 总用量 5  
  14. drwxrwxrwx  1 root root    0 2014-03-25 23:51 .  
  15. drwxrwxrwx  1 root root    0 2014-03-25 23:10 ..  
  16. -rwxrwxrwx  1 root root  219 2014-03-25 23:30 Makefile  
  17. -rwxrwxrwx  1 root root 6803 2014-03-25 23:51 moda  
  18. -rwxrwxrwx  1 root root  149 2014-03-25 23:39 moda.c  
  19. -rwxrwxrwx  1 root root    0 2014-03-25 23:10 moda.h  
  20. -rwxrwxrwx  1 root root  908 2014-03-25 23:51 moda.o  
  21. linux:/mnt/hgfs/vmware-share/makefile/src/moda # <strong><span style="color:#000099;">./moda</span></strong>  
  22. This is mod A, MAX_SIZE:10  
  23. <pre code_snippet_id="256920" snippet_file_name="blog_20140325_5_2131557" name="code" class="cpp">linux:/mnt/hgfs/vmware-share/makefile #  
  24. linux:/mnt/hgfs/vmware-share/makefile #  
  25. linux:/mnt/hgfs/vmware-share/makefile #</pre>linux:/mnt/hgfs/vmware-share/makefile # <strong><span style="color:#000099">make clean</span></strong><br>  
  26. make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/moda'<br>  
  27. rm *.o moda<br>  
  28. make[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/moda'<br>  
  29. make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/modb'<br>  
  30. rm *.o modb<br>  
  31. make[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/modb'<br>  
posted @ 2017-06-23 10:13  司空落星  阅读(1739)  评论(0编辑  收藏  举报