下面是我在一个项目中使用的Makefile.
脚本会自动搜索当前目录下所有子目录,并依据目录下的.c 和 .cxx生成对应的.o,最后生成应用application,
代码中删除了项目相关配置,如CFLAGS,LDFLAGS,CXXFLAGS中的gcc配置
欢迎转载,烦请添加链接,谢谢!
1 SHELL=/bin/sh
2
3 CC = $(CROSS_COMPILE)gcc
4 CXX = $(CROSS_COMPILE)g++
5 LD = $(CROSS_COMPILE)ld
6
7 CFLAGS +=
8
9 LDFLAGS +=-L.
10
11 MAKE_DIR=$(PWD)
12
13 CFLAGS += $(INCLS)
14 CXXFLAGS += $(INCLS)
15 #find all the sub-directory
16 VPATH=$(foreach dir,$(shell find . -type d),$(shell echo $(dir):))
17 #include all the sub-directory
18 INCLS=$(foreach dir,$(subst :, ,$(VPATH)),$(shell echo -I$(dir)))
19 #find all .c and produce .o
20 C_SRC_PATH = $(foreach dir,$(subst :, ,$(VPATH)),$(wildcard $(dir)/*.c))
21 COBJS = $(subst .c,.o,$(C_SRC_PATH))
22 #find all .cxx and produce .o
23 CPP_SRC_PATH = $(foreach dir,$(subst :, ,$(VPATH)),$(wildcard $(dir)/*.cxx))
24 CPPOBJS = $(subst .cxx,.o,$(CPP_SRC_PATH))
25
26 execobjs = $(COBJS) $(CPPOBJS)
27 #the scripts for compile
28 .PHONY: all
29
30 all: $(execobjs)
31 $(CXX) -o application $(execobjs) $(LDFLAGS)
32
33 $(COBJS):%.o:%.c
34 $(CC) -c $< -o $@ $(CFLAGS)
35
36 $(CPPOBJS):%.o:%.cc
37 $(CC) -c $< -o $@ $(CXXFLAGS)
38 #the scripts for clean
39 .PHONY: clean
40
41 clean:
42 rm application $(execobjs)