Chinaunix首页 | 论坛 | 博客
  • 博客访问: 67006
  • 博文数量: 17
  • 博客积分: 263
  • 博客等级: 二等列兵
  • 技术积分: 175
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-17 08:54
文章分类
文章存档

2012年(15)

2011年(2)

我的朋友

分类: 嵌入式

2012-02-20 08:51:58

一下是一个稍微完整的Makefile文件,文件编译的工程一共有四个模块:array,foo,hello,Main;
头文件在inc中,源码和子Makefile在src目录中。
  1. #主Makefile
  2. #环境变量,目标路径,进入子目录make all clean,总的make all clean,打印完成信息

  3. .PHONY:all clean

  4. MAKE = make
  5. MAKEFLAG = -r

  6. RM = rm
  7. RMFLAG = -rf

  8. #ROOT是上一目录的绝对路径
  9. ROOT = $(realpath ..)
  10. DIR = $(ROOT)/code/foo/src $(ROOT)/code/hello/src $(ROOT)/code/array/src $(ROOT)/code/Main/src

  11. RMS = $(ROOT)/build/exes $(ROOT)/build/libs

  12. #1.生成的目标与依赖关系集合
  13. #循环 cd make clean
  14. all clean:
  15.     @echo "Make $@ begin!"
  16.     @set -e ;\
  17.     for dir in $(DIR) ;\
  18.     do \
  19.         cd $$dir && $(MAKE) -r ROOT=$(ROOT) $@ ;\
  20.     done
  21.     @set -e ;\
  22.     if [ "$(MAKECMDGOALS)" = "clean" ]; \
  23.         then $(RM) $(RMFLAG) $(RMS); \
  24.     fi
  25.     @echo ""
  26.     @echo "~) Completed"
  27.     @echo ""

  28. #先必须执行完毕所有的子Makefile中的all clean规则,再执行总的all clean规则,这在26行
  29. #在这一点上无论是all clean都一样,故有一致性,可以写在一起
主Makefile
  1. #公有Makefile

  2. .PHONY:all clean

  3. CC = gcc
  4. RM = rm
  5. RMFLAG = -rf
  6. MKDIR = mkdir
  7. AR = ar
  8. ARFLAG = crs

  9. #3.路径
  10. #绝对路径
  11. DIR_EXE = $(ROOT)/build/exes
  12. DIR_LIBS = $(ROOT)/build/libs
  13. #相对路径
  14. DIR_DEPS = deps
  15. DIR_OBJS = objs
  16. DIRS = $(DIR_DEPS) $(DIR_EXE) $(DIR_LIBS) $(DIR_OBJS)

  17. #3**.RMS clean:exe .a .o .dep文件
  18. #每个子Makefile应该清理掉的战场: .o和.dep文件
  19. RMS = $(DIR_DEPS) $(DIR_OBJS)

  20. #2.目标
  21. #EXE =
  22. #LIBS =
  23. ifneq ("$(LIBS)","")
  24. LIBS := $(addprefix $(DIR_LIBS)/,$(LIBS))
  25. RMS += $(LIBS)
  26. endif
  27. ifneq ("$(EXE)","")
  28. EXE := $(addprefix $(DIR_EXE)/,$(EXE))
  29. RMS += $(EXE)
  30. endif

  31. #4.源
  32. SRCS = $(wildcard *.c)
  33. OBJS = $(SRCS:.c=.o)
  34. OBJS := $(addprefix $(DIR_OBJS)/,$(OBJS))
  35. #依赖关系文件加上路径
  36. DEPS = $(SRCS:.c=.dep)
  37. DEPS := $(addprefix $(DIR_DEPS)/,$(DEPS))

  38. #6.LINK_LIBS与INCLUDE_DIRS
  39. #-I *.h在生成.o文件的方法里和依赖关系里面都用到了,一共两处,INCLUDE_DIRS变量在子Makefile中指定
  40. #-l *.a $(LINK_LIBS)与$(DEP_LIBS)
  41. #做出一个libX%的筛选器,找出$(DIR_LIBS)/里面的所有库文件
  42. ifneq ("$(LINK_LIBS)","")
  43. LINK_LIBS := $(strip $(LINK_LIBS))
  44. LINK_LIBS := $(addprefix -l,$(LINK_LIBS))
  45. LIB_ALL := $(notdir $(wildcard $(DIR_LIBS)/*))
  46. LIB_FILTERED := $(addsuffix %, $(addprefix lib,$(LINK_LIBS)))
  47. $(eval DEP_LIBS=$(filter $(LIB_FILTERED),$(LIB_ALL)))
  48. DEP_LIBS := $(addprefix $(DIR_LIBS)/,$(DEP_LIBS))
  49. endif
  50. ifneq ("$(INCLUDE_DIRS)","")
  51. INCLUDE_DIRS := $(strip $(INCLUDE_DIRS))
  52. INCLUDE_DIRS := $(addprefix -I,$(INCLUDE_DIRS))
  53. endif

  54. #5.DEP_DIR_EXE
  55. #目录不存在则新建目录
  56. #使用ifeq [ "$(DEP_DIR_EXE)","" ],ifeq [ "$(DEP_DIR_EXE" = "" ]是不行的,每次运行Makefile $(DEP_DIR_EXE)都是空,变量是空与文件夹不存在是两回事
  57. ifeq ("$(wildcard $(DIR_EXE))","")
  58. DEP_DIR_EXE := $(DIR_EXE)
  59. endif
  60. ifeq ("$(wildcard $(DIR_OBJS))","")
  61. DEP_DIR_OBJS := $(DIR_OBJS)
  62. endif
  63. ifeq ("$(wildcard $(DIR_LIBS))","")
  64. DEP_DIR_LIBS := $(DIR_LIBS)
  65. endif
  66. ifeq ("$(wildcard $(DIR_DEPS))","")
  67. DEP_DIR_DEPS := $(DIR_DEPS)
  68. endif

  69. #1.生成关系目标依赖
  70. #include与mkdir
  71. #all与DEP_LIBS
  72. all:$(EXE) $(LIBS)
  73. ifneq ($(MAKECMDGOALS),clean)
  74. include $(DEPS)
  75. endif
  76. $(DIRS):
  77. $(MKDIR) $@
  78. $(EXE):$(DEP_DIR_EXE) $(OBJS) $(DEP_LIBS)
  79. $(CC) -L $(DIR_LIBS) -o $@ $(filter %.o,$^) $(LINK_LIBS)
  80. $(DIR_OBJS)/%.o:$(DEP_DIR_OBJS) %.c
  81. $(CC) $(INCLUDE_DIRS) -o $@ -c $(filter %.c,$^)
  82. $(LIBS):$(DEP_DIR_LIBS) $(OBJS)
  83. $(AR) $(ARFLAG) $@ $(filter %.o,$^)

  84. #7.dep依赖关系规则,以便include生成,dep文件
  85. $(DIR_DEPS)/%.dep:$(DEP_DIR_DEPS) %.c
  86. @echo "Creating $@ ..."
  87. @set -e ; \
  88. $(RM) $(RMFLAG) $@.tmp ; \
  89. $(CC) $(INCLUDE_DIRS) -E -MM $(filter %.c,$^) > $@.tmp ; \
  90. sed 's,\(.*\)\.o[ :]*,objs/\1.o $@: ,g' < $@.tmp > $@ ; \
  91. $(RM) $(RMFLAG) $@.tmp

  92. #8.clean
  93. clean:
  94. $(RM) $(RMFLAG) $(RMS)
公有Makefile


  1. #子Makefile模板
  2. #子目标 .h 库 包含公有Makefile

  3. EXE =
  4. LIBS = libarray.a

  5. INCLUDE_DIRS = $(ROOT)/code/array/inc
  6. LINK_LIBS =
  7. include $(ROOT)/build/MakefileRule
子Makefilearray模块


  1. #子Makefile模板
  2. #子目标 .h 库 包含公有Makefile

  3. EXE =
  4. LIBS = libfoo.a

  5. INCLUDE_DIRS = $(ROOT)/code/foo/inc
  6. LINK_LIBS =
  7. include $(ROOT)/build/MakefileRule
子Makefilefoo模块

  1. #子Makefile模板
  2. #子目标 .h 库 包含公有Makefile

  3. #ROOT = /home/qq/learning/MakefileModel/

  4. EXE =
  5. LIBS = libhello.a

  6. INCLUDE_DIRS = $(ROOT)/code/hello/inc
  7. LINK_LIBS =
  8. include $(ROOT)/build/MakefileRule
子Makefilehello模块
  1. #子Makefile模板
  2. #子目标 .h 库 包含公有Makefile

  3. EXE = complicated
  4. LIBS =

  5. INCLUDE_DIRS = $(ROOT)/code/array/inc $(ROOT)/code/foo/inc $(ROOT)/code/hello/inc
  6. LINK_LIBS = array foo hello
  7. include $(ROOT)/build/MakefileRule
子MakefileMain MakefileHuge.tar.gz.rar   模块

阅读(4708) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~