Makefile topic: A comprehensive example of variables and functions. makefile topic
1. Actual needs
1) Automatic GenerationTarget folderStore executable files
2) Automatic GenerationObjs folderStore the compiled target file (*. o)
3) supportedDebug versionCompilation options
4) Considering code scalability
2. Tool Raw Materials
$ (Wildcard_parrern ):Obtain the conditions in the current working directory_ Pattern file or directory list
$ (Addprefix _ prefix, _ names ):Name List_ Adding a prefix for each name in names_prefix
3. Key Skills Automatically obtain the source file list in the current directory(Function call ):SRCS: = $ (wildcard *. c)Generate a target file list based on the source file list(Replace the variable value ):OBJS: =$ (SRCS:. c =. o)Add the path prefix to each target file list(Function call ):OBJS := $(addprefix path/, $(OBJS)) 4. Rule mode replacement (directory structure)
### 5. Compile the rule dependency
CC := gccMKDIR := mkdirRM := rm -frDIR_OBJS := objsDIR_TARGET := targetDIRS := $(DIR_OBJS) $(DIR_TARGET)TARGET := $(DIR_TARGET)/hello-makefile.outSRCS := $(wildcard *.c)OBJS := $(SRCS:.c=.o)OBJS := $(addprefix $(DIR_OBJS)/, $(OBJS)).PHONY : rebuild clean all$(TARGET) : $(DIRS) $(OBJS) $(CC) -o $@ $(OBJS) @echo "Target File ==> $@"$(DIRS) : $(MKDIR) $@$(DIR_OBJS)/%.o : %.c ifeq ($(DEBUG),true) $(CC) -o $@ -g -c $^ else $(CC) -o $@ -c $^ endifrebuild : clean all all : $(TARGET)clean : $(RM) $(DIRS)