Makefile topic: A comprehensive example of variables and functions. makefile topic

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.