第二十二课 打造专业的编译环境(下)
Posted wanmeishenghuo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二十二课 打造专业的编译环境(下)相关的知识,希望对你有一定的参考价值。
顶层目录如下:
顶层makefile:
1 include pro-cfg.mk 2 include cmd-cfg.mk 3 include pro-rule.mk
cmd-cfg.mk:
1 AR := ar 2 ARFLAGS := crs 3 4 CC := gcc 5 LFLAGS := 6 CFLAGS := -I$(DIR_INC) -I$(DIR_COMMON_INC) 7 8 ifeq ($(DEBUG),true) 9 CFLAGS += -g 10 endif 11 12 MKDIR := mkdir 13 RM := rm -fr
mod-cfg.mk:
1 DIR_SRC := src 2 DIR_INC := inc 3 4 TYPE_INC := .h 5 TYPE_SRC := .c 6 TYPE_OBJ := .o 7 TYPE_DEP := .dep
mode-rule.mk:
1 .PHONY : all 2 3 MODULE := $(realpath .) 4 MODULE := $(notdir $(MODULE)) 5 DIR_OUTPUT := $(addprefix $(DIR_BUILD)/, $(MODULE)) 6 7 OUTPUT := $(MODULE).a 8 OUTPUT := $(addprefix $(DIR_BUILD)/, $(OUTPUT)) 9 10 SRCS := $(wildcard $(DIR_SRC)/*$(TYPE_SRC)) 11 OBJS := $(SRCS:$(TYPE_SRC)=$(TYPE_OBJ)) 12 OBJS := $(patsubst $(DIR_SRC)/%, $(DIR_OUTPUT)/%, $(OBJS)) 13 DEPS := $(SRCS:$(TYPE_SRC)=$(TYPE_DEP)) 14 DEPS := $(patsubst $(DIR_SRC)/%, $(DIR_OUTPUT)/%, $(DEPS)) 15 16 vpath %$(TYPE_INC) $(DIR_INC) 17 vpath %$(TYPE_INC) $(DIR_COMMON_INC) 18 vpath %$(TYPE_SRC) $(DIR_SRC) 19 20 -include $(DEPS) 21 22 all : $(OUTPUT) 23 @echo "Success! Target ==> $(OUTPUT)" 24 25 $(OUTPUT) : $(OBJS) 26 $(AR) $(ARFLAGS) [email protected] $^ 27 28 $(DIR_OUTPUT)/%$(TYPE_OBJ) : %$(TYPE_SRC) 29 $(CC) $(CFLAGS) -o [email protected] -c $(filter %$(TYPE_SRC), $^) 30 31 32 $(DIR_OUTPUT)/%$(TYPE_DEP) : %$(TYPE_SRC) 33 @echo "Creating [email protected] ..." 34 @set -e; 35 $(CC) $(CFLAGS) -MM -E $(filter %$(TYPE_SRC), $^) | sed ‘s,(.*).o[ :]*,$(DIR_OUTPUT)/1$(TYPE_OBJ) [email protected] : ,g‘ > [email protected]
pro-cfg.mk:
1 MODULES := common 2 module 3 main 4 5 MOD_CFG := mod-cfg.mk 6 MOD_RULE := mod-rule.mk 7 CMD_CFG := cmd-cfg.mk 8 9 DIR_BUILD := build 10 DIR_COMMON_INC := common/inc 11 12 APP := app.out
pro-rule.mk:
1 .PHONY : all compile link clean rebuild 2 3 DIR_PROJECT := $(realpath .) 4 DIR_BUILD_SUB := $(addprefix $(DIR_BUILD)/, $(MODULES)) 5 MODULE_LIB := $(addsuffix .a, $(MODULES)) 6 MODULE_LIB := $(addprefix $(DIR_BUILD)/, $(MODULE_LIB)) 7 8 9 APP := $(addprefix $(DIR_BUILD)/, $(APP)) 10 11 all : compile $(APP) 12 @echo "Success! Target ==> $(APP)" 13 14 compile : $(DIR_BUILD) $(DIR_BUILD_SUB) 15 @echo "Begin to compile ..." 16 @set -e; 17 for dir in $(MODULES); 18 do 19 cd $$dir && 20 $(MAKE) all 21 DEBUG:=$(DEBUG) 22 DIR_BUILD:=$(addprefix $(DIR_PROJECT)/, $(DIR_BUILD)) 23 DIR_COMMON_INC:=$(addprefix $(DIR_PROJECT)/, $(DIR_COMMON_INC)) 24 CMD_CFG:=$(addprefix $(DIR_PROJECT)/, $(CMD_CFG)) 25 MOD_CFG:=$(addprefix $(DIR_PROJECT)/, $(MOD_CFG)) 26 MOD_RULE:=$(addprefix $(DIR_PROJECT)/, $(MOD_RULE)) && 27 cd .. ; 28 done 29 @echo "Compile Success!" 30 31 link $(APP) : $(MODULE_LIB) 32 @echo "Begin to link ..." 33 $(CC) -o $(APP) -Xlinker "-(" $^ -Xlinker "-)" $(LFLAGS) 34 @echo "Link Success!" 35 36 $(DIR_BUILD) $(DIR_BUILD_SUB) : 37 $(MKDIR) [email protected] 38 39 clean : 40 @echo "Begin to clean ..." 41 $(RM) $(DIR_BUILD) 42 @echo "Clean Success!" 43 44 rebuild : clean all
module文件夹下的makefile:
1 include $(MOD_CFG) 2 3 # Custmization Begin 4 # 5 # DIR_SRC := src 6 # DIR_INC := inc 7 # 8 # TYPE_INC := .h 9 # TYPE_SRC := .c 10 # TYPE_OBJ := .o 11 # TYPE_DEP := .dep 12 # 13 # Custmization End 14 15 include $(CMD_CFG) 16 17 include $(MOD_RULE)
main文件夹下的makefile:
1 include $(MOD_CFG) 2 3 # Custmization Begin 4 # 5 # DIR_SRC := src 6 # DIR_INC := inc 7 # 8 # TYPE_INC := .h 9 # TYPE_SRC := .c 10 # TYPE_OBJ := .o 11 # TYPE_DEP := .dep 12 # 13 # Custmization End 14 15 include $(CMD_CFG) 16 17 include $(MOD_RULE)
common文件夹下的makefile:
1 include $(MOD_CFG) 2 3 # Custmization Begin 4 # 5 # DIR_SRC := src 6 # DIR_INC := inc 7 # 8 # TYPE_INC := .h 9 # TYPE_SRC := .c 10 # TYPE_OBJ := .o 11 # TYPE_DEP := .dep 12 # 13 # Custmization End 14 15 include $(CMD_CFG) 16 17 include $(MOD_RULE)
以上是关于第二十二课 打造专业的编译环境(下)的主要内容,如果未能解决你的问题,请参考以下文章