Makefile 的写作思路小结:
1)伪目标
2)命令(CC MKDIR...)
3)目录(绝对路径、相对路径、临时目录、条件加入的目录...)
4)实际目标(对空目标分开处理)
5)include(写在all之下)
6)创建目录(对写好的目录加上条件并创建、同时可选择加入依赖中)
7)实际目标的生成方法
8)完善依赖关系
9)头文件的查找与链接
10)库文件依赖关系的裁剪与添加
11)自动生成依赖关系并修改加入前缀(经典六句话)
12)提取公共部分至make.rule文件中并修改模块生成文件的路径
13)各个子模块的目标库文件的写入与删除
14)编写总体Makefile放在指定的build目录中
注:一下程序都来源于《专业嵌入式软件开发》一书,李云著
- .PHONY:all clean
-
-
CC = gcc
-
RM = rm
-
RMFLAGS = -rf
-
MKDIR = mkdir
-
AR = ar
-
ARFLAGS = crs
-
-
DIR_OBJS = objs
-
DIR_DEPS = deps
-
DIR_LIBS = ../../../libs
-
DIR_EXES = ../../../exes
-
DIRS = $(DIR_OBJS) $(DIR_DEPS) $(DIR_LIBS) $(DIR_EXES)
-
RMS = $(DIR_DEPS) $(DIR_OBJS)
-
-
LIB = libfoo.a
-
ifneq ("$(LIB)","")
-
LIB := $(addprefix $(DIR_LIBS)/,$(LIB))
-
RMS += $(LIB)
-
endif
-
-
EXE = complicated
-
ifneq ("$(EXE)","")
-
EXE := $(addprefix $(DIR_EXES)/,$(EXE))
-
RMS += $(EXE)
-
endif
-
-
SRCS = $(wildcard *.c)
-
OBJS = $(SRCS:.c=.o)
-
OBJS := $(addprefix $(DIR_OBJS)/,$(OBJS))
-
DEPS = $(SRCS:.c=.dep)
-
DEPS := $(addprefix $(DIR_DEPS)/,$(DEPS))
-
-
#如下写法只是在判断DIR_EXES这个变量是不是空,而不是../../../
-
#ifeq("$(DIR_EXES)","")
-
-
ifeq ("$(wildcard $(DIR_EXES))","")
-
DEP_DIR_EXES := $(DIR_EXES)
-
endif
-
ifeq ("$(wildcard $(DIR_OBJS))","")
-
DEP_DIR_OBJS := $(DIR_OBJS)
-
endif
-
ifeq ("$(wildcard $(DIR_DEPS))","")
-
DEP_DIR_DEPS := $(DIR_DEPS)
-
endif
-
ifeq ("$(wildcard $(DIR_LIBS))","")
-
DEP_DIR_LIBS := $(DIR_LIBS)
-
endif
-
-
all:$(EXE) $(LIB)
-
-
ifneq ($(MAKECMDGOALS),clean)
-
include $(DEPS)
-
endif
-
-
$(DIRS):
-
$(MKDIR) $@
-
-
$(EXE): $(DEP_DIR_EXES) $(OBJS)
-
$(CC) -o $@ $(filter %.o,$^)
-
$(LIB): $(DEP_DIR_LIBS) $(OBJS)
-
$(AR) $(ARFLAGS) $@ $(filter %.o,$^)
-
$(DIR_OBJS)/%.o: $(DEP_DIR_OBJS) %.c
-
$(CC) -o $@ -c $(filter %.c,$^)
-
$(DIR_DEPS)/%.dep: $(DEP_DIR_DEPS) %.c
-
@echo "Creating $@ ..."
-
@set -e ; \
-
$(RM) $(RMFLAGS) $@.tmp ; \
-
$(CC) -E -MM $(filter %.c,$^) > $@.tmp ; \
-
sed 's,\(.*\)\.o[ :]*,objs/\1.o $@: ,g' < $@.tmp > $@ ; \
-
$(RM) $(RMFLAGS) $@.tmp
-
-
clean:
-
$(RM) $(RMFLAGS) $(RMS)
阅读(849) | 评论(0) | 转发(0) |