第二十四课 第三方库的使用支持
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) -I$(DIR_LIBS_INC) 7 8 ifeq ($(DEBUG),true) 9 CFLAGS += -g 10 endif 11 12 MKDIR := mkdir 13 RM := rm -fr 14 CP := cp
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
mod-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_INC) $(DIR_LIBS_INC) 19 vpath %$(TYPE_SRC) $(DIR_SRC) 20 21 -include $(DEPS) 22 23 all : $(OUTPUT) 24 @echo "Success! Target ==> $(OUTPUT)" 25 26 $(OUTPUT) : $(OBJS) 27 $(AR) $(ARFLAGS) [email protected] $^ 28 29 $(DIR_OUTPUT)/%$(TYPE_OBJ) : %$(TYPE_SRC) 30 $(CC) $(CFLAGS) -o [email protected] -c $(filter %$(TYPE_SRC), $^) 31 32 33 $(DIR_OUTPUT)/%$(TYPE_DEP) : %$(TYPE_SRC) 34 @echo "Creating [email protected] ..." 35 @set -e; 36 $(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 DIR_LIBS_INC := libs/inc 12 DIR_LIBS_LIB := libs/lib 13 14 APP := app.out
pro-rule.mk:
1 .PHONY : all compile link clean rebuild $(MODULES) 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 EXTERNAL_LIB := $(wildcard $(DIR_LIBS_LIB)/*) 8 EXTERNAL_LIB := $(patsubst $(DIR_LIBS_LIB)/%, $(DIR_BUILD)/%, $(EXTERNAL_LIB)) 9 10 APP := $(addprefix $(DIR_BUILD)/, $(APP)) 11 12 define makemodule 13 cd $(1) && 14 $(MAKE) all 15 DEBUG:=$(DEBUG) 16 DIR_BUILD:=$(addprefix $(DIR_PROJECT)/, $(DIR_BUILD)) 17 DIR_COMMON_INC:=$(addprefix $(DIR_PROJECT)/, $(DIR_COMMON_INC)) 18 DIR_LIBS_INC:=$(addprefix $(DIR_PROJECT)/, $(DIR_LIBS_INC)) 19 CMD_CFG:=$(addprefix $(DIR_PROJECT)/, $(CMD_CFG)) 20 MOD_CFG:=$(addprefix $(DIR_PROJECT)/, $(MOD_CFG)) 21 MOD_RULE:=$(addprefix $(DIR_PROJECT)/, $(MOD_RULE)) && 22 cd .. ; 23 endef 24 25 all : compile $(APP) 26 @echo "Success! Target ==> $(APP)" 27 28 compile : $(DIR_BUILD) $(DIR_BUILD_SUB) 29 @echo "Begin to compile ..." 30 @set -e; 31 for dir in $(MODULES); 32 do 33 $(call makemodule, $$dir) 34 done 35 @echo "Compile Success!" 36 37 link $(APP) : $(MODULE_LIB) $(EXTERNAL_LIB) 38 @echo "Begin to link ..." 39 $(CC) -o $(APP) -Xlinker "-(" $^ -Xlinker "-)" $(LFLAGS) 40 @echo "Link Success!" 41 42 $(DIR_BUILD)/% : $(DIR_LIBS_LIB)/% 43 $(CP) $^ [email protected] 44 45 $(DIR_BUILD) $(DIR_BUILD_SUB) : 46 $(MKDIR) [email protected] 47 48 clean : 49 @echo "Begin to clean ..." 50 $(RM) $(DIR_BUILD) 51 @echo "Clean Success!" 52 53 rebuild : clean all 54 55 $(MODULES) : $(DIR_BUILD) $(DIR_BUILD)/$(MAKECMDGOALS) 56 @echo "Begin to compile [email protected]" 57 @set -e; 58 $(call makemodule, [email protected]) 59
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)
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)
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)
libs文件夹下内容:
inc下内容:
lib下内容:
以上是关于第二十四课 第三方库的使用支持的主要内容,如果未能解决你的问题,请参考以下文章