Article From:https://www.cnblogs.com/wanmeishenghuo/p/9735204.html

 

 

 

 

 

The code directory is as follows:

The top level makefile is as follows: there is no makefile in each folder.

 

 1 .PHONY : all
 2 
 3 DIR_BUILD := /home/delphi/make/build
 4 DIR_COMMON_INC := /home/delphi/make/common/inc
 5 
 6 DIR_SRC := src
 7 DIR_INC := inc
 8 
 9 TYPE_INC := .h
10 TYPE_SRC := .c
11 TYPE_OBJ := .o
12 TYPE_DEP := .dep
13 
14 AR := ar
15 ARFLAGS := crs
16 
17 CC := gcc
18 CFLAGS := -I$(DIR_INC) -I$(DIR_COMMON_INC)
19 
20 ifeq ($(DEBUG),true)
21 CFLAGS += -g
22 endif
23 
24 MODULE := $(realpath .)
25 MODULE := $(notdir $(MODULE))
26 DIR_OUTPUT := $(addprefix $(DIR_BUILD)/, $(MODULE))
27 
28 OUTPUT := $(MODULE).a
29 OUTPUT := $(addprefix $(DIR_BUILD)/, $(OUTPUT))
30 
31 SRCS := $(wildcard $(DIR_SRC)/*$(TYPE_SRC))
32 OBJS := $(SRCS:$(TYPE_SRC)=$(TYPE_OBJ))
33 OBJS := $(patsubst $(DIR_SRC)/%, $(DIR_OUTPUT)/%, $(OBJS))
34 DEPS := $(SRCS:$(TYPE_SRC)=$(TYPE_DEP))
35 DEPS := $(patsubst $(DIR_SRC)/%, $(DIR_OUTPUT)/%, $(DEPS))
36 
37 vpath %$(TYPE_INC) $(DIR_INC)
38 vpath %$(TYPE_INC) $(DIR_COMMON_INC)
39 vpath %$(TYPE_SRC) $(DIR_SRC)
40 
41 -include $(DEPS)
42 
43 all : $(OUTPUT)
44     @echo "Success! Target ==> $(OUTPUT)"
45     
46 $(OUTPUT) : $(OBJS)
47     $(AR) $(ARFLAGS) $@ $^
48     
49 $(DIR_OUTPUT)/%$(TYPE_OBJ) : %$(TYPE_SRC)
50     $(CC) $(CFLAGS) -o $@ -c $(filter %$(TYPE_SRC), $^)
51     
52     
53 $(DIR_OUTPUT)/%$(TYPE_DEP) : %$(TYPE_SRC)
54     @echo "Creating $@ ..."
55     @set -e; \
56     $(CC) $(CFLAGS) -MM -E $(filter %$(TYPE_SRC), $^) | sed 's,\(.*\)\.o[ :]*,$(DIR_OUTPUT)/\1$(TYPE_OBJ) $@ : ,g' > $@

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *