编写包含多文件的Makefile以及Makefile的嵌套实验

来源:互联网 发布:sql调用存储过程 编辑:程序博客网 时间:2024/05/17 21:47

(1)创建目录结构

(2)输入"tree"命令,查看目录;若没有则安装tree命令 (sudo apt-get install tree )


[html] view plaincopyprint?
  1. //f1.c  
  2.   
  3. #include "../include/myinclude.h"                                                                              
  4. void print1()  
  5. {  
  6.     printf("Message f1.c\n");  
  7.     return;  
  8. }  

[html] view plaincopyprint?
  1. //Makefile (/f1目录下的)  
  2.   
  3. ../$(OBJS_DIR)/f1.o:f1.c                                                                                       
  4.     $(CC) -c $^ -o $@  
f2.c 和f2目录下的Makefle和f1目录下的除名字外内容相同
[html] view plaincopyprint?
  1. //main.c   
  2.   
  3. #include <stdio.h>                                                                                             
  4. int main()  
  5. {  
  6.     print1();  
  7.     print2();  
  8.   
  9.     return 0;  
  10. }  
[html] view plaincopyprint?
  1. //Makefile (/man目录下的Makefile)  
  2.   
  3. ../$(OBJS_DIR)/main.o:main.c                                                                                   
  4.     $(CC) -c $^ -o $@  

[html] view plaincopyprint?
  1. //顶层目录下的Makeflie文件  
  2. cc=gcc                                                                                                         
  3. SUBDIRS=f1 \  
  4.         f2 \  
  5.         main \  
  6.         obj  
  7. OBJS=f1.o f2.o main.o  
  8. BIN=myapp  
  9. OBJS_DIR=obj  
  10. BIN_DIR=bin  
  11. export CC OBJS BIN OBJS_DIR BIN_DIR  
  12.   
  13. all:CHECK_DIR $(SUBDIRS)  
  14. CHECK_DIR:  
  15.     mkdir -p $(BIN_DIR)  
  16. $(SUBDIRS):ECHO  
  17.     make -C $@  
  18. ECHO:  
  19.     @echo $(SUBDIRS)  
  20.     @echo begin compile  
  21. CLEAN:  
  22.     @$(RM) $(OBJS_DIR)/*.o  
  23.     @rm -rf $(BIN_DIR)  
[html] view plaincopyprint?
  1. //Obj目录下的Makefile  
  2.   
  3. ../$(BIN_DIR)/$(BIN):$(OBJS)                                                                                   
  4.     $(CC) -o $@ $^  
[html] view plaincopyprint?
  1. include目录下的myinclude.h 文件  
  2. #include <stdio.h>   
显示效果:


原文地址:http://blog.csdn.net/zplove003/article/details/7066595