Write the project makefile from scratch (9): Directory Search

Source: Internet
Author: User

[Copyright statement: reprinted. Please retain the Source: blog.csdn.net/gentleliu. Mail: shallnew at 163 dot com]

In a large project, the source code and binary files (. O files and executable files) are arranged in different directories for differentiated management. In this case, you can use the Directory Search dependency file function provided by make (automatically search for dependent files under several specified directories ). In makefile, use the directory search function of dependent files. When the directory structure of the project changes, you can do not change the makefile rules, only change the search directory of the dependent files.

In the problems we encountered in the previous section, we will. c files are stored in the src directory and are not in the same directory as the makefile directory. Therefore, there is no way to find the C files. o. The make program has a special variable vpath, which can specify the search path of dependent files. When the rule dependency file does not exist in the current directory, make searches for these dependent files in the directory specified by this variable. This variable is usually used to specify the search path of the Rule dependent file.
When defining the variable "vpath", multiple directories to be searched are separated by spaces or colons. Make searches for directories in the order defined by the variable "vpath". The current directory is always the first. For example
VPATH += ./src
The dependency search directory is specified as the src directory under the current directory. in rules, assign a value to the vpath variable and include the makefile. the current module is provided before rules. directory where the c file is located.
In fact, we can also directly specify the path of the dependent file, which is also possible, as follows:
$(SRC_OBJ) : $(OBJDIR)/%.o : $(MOD_SRC_DIR)/%.c                                                                                                                          >---$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o [email protected]
However, after we have changed the project directory structure, the corresponding dependent files are not in the same directory and become troublesome. Therefore, it is better to assign a value to the vpath variable directly, you only need to specify the directory where the source code is located.
In fact, there is another method for searching the file path: using the vpath keyword (note that it is not a vpath variable), it is similar to vpath, but it can be different types of files (separated by file names) specify different search directories. There are three methods:
1. vpath pattern Directories
Specify the search directory "directories" for all files that match the pattern ". Separate multiple directories with spaces or colons.
2. vpath Pattern
Clear the search path set for the file that matches the pattern.
3. vpath
Clear all preset file search paths.
In the vapth usage method, "pattern" must contain the pattern character "%". For example, the preceding definition:
VPATH += ./src
It can be written as follows:
vpath %.c ./src
Now let's give a makefile. Rules:
# Copyright (C) 2014 shallnew \at 163 \dot com                                                                                                                           # if without a platform defined, give value "unknow" to PLATFORMifndef PLATFORM>---PLATFORM = unknowendif# define a root build directory base on the platform# if without a SRC_BASE defined, just use local src directoryifeq ($(SRC_BASE),)>---BUILDDIR = $(MOD_SRC_DIR)>---OBJDIR = $(MOD_SRC_DIR)>---LIBDIR = $(MOD_SRC_DIR)>---BINDIR = $(MOD_SRC_DIR)else>---ifeq ($(DEBUG_SYMBOLS), TRUE)>--->---BUILDDIR = $(SRC_BASE)/build/$(PLATFORM)_dbg>---else>--->---BUILDDIR = $(SRC_BASE)/build/$(PLATFORM)>---endif>---OBJDIR = $(BUILDDIR)/obj/$(MODULE)>---LIBDIR = $(BUILDDIR)/lib>---BINDIR = $(BUILDDIR)/binendif# update compilation flags base on "DEBUG_SYMBOLS"ifeq ($(DEBUG_SYMBOLS), TRUE)>---CFLAGS += -g -Wall -Werror -O0else>---CFLAGS += -Wall -Werror -O2endifVPATH += $(MOD_SRC_DIR)SRC_OBJ = $(patsubst %.c, $(OBJDIR)/%.o, $(notdir $(SRC_FILES)))ifeq ($(MAKELEVEL), 0)all : msgelseall : lib binendiflib : $(OBJDIR) $(LIBDIR)/$(SRC_LIB)bin : $(OBJDIR) $(BINDIR)/$(SRC_BIN)$(OBJDIR) :>---mkdir -p [email protected]ifneq ($(SRC_BIN),)$(BINDIR)/$(SRC_BIN) : $(SRC_OBJ)>---$(CC) -o [email protected] $^ $(LDFLAGS)endififneq ($(SRC_LIB),)$(LIBDIR)/$(SRC_LIB) : $(SRC_OBJ)>---$(AR) rcs [email protected] $^>---cp [email protected] $(SRC_BASE)/libsendif$(SRC_OBJ) : $(OBJDIR)/%.o : %.c>---$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o [email protected]msg:>[email protected] "You cannot directily execute this Makefile! This Makefile should called by toplevel Makefile."# clean targetclean:ifneq ($(SRC_LIB),)>--->---$(RM) $(SRC_OBJ) $(LIBDIR)/$(SRC_LIB)endififneq ($(SRC_BIN),)>--->---$(RM) $(SRC_OBJ) $(BINDIR)/$(SRC_BIN)endif.PHONY : all clean    



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.