makefile--Catalog Search (eight)

Source: Internet
Author: User

In a larger project, the source code and binaries (. o files and executables) are typically arranged in different directories for differentiated management. In this case, we can use the Directory search dependent file feature provided by make (to automatically search for dependent files under a specified number of directories). In makefile, use the directory Search feature for dependent files.     When the project directory structure changes, you can do not change the rules of makefile, only the dependent files to change the search directory. In our previous section of the problem, we put. c files in the SRC directory, without the makefile directory in the same directory, so there is no way to find the. o File dependent files. The Make program has a special variable Vpath that specifies the search path to the dependent file, and make will look for these dependent files in the directory specified by this variable when the dependent file for the rule does not exist in the current directory. Usually we are using this variable to specify the rule's dependent file search path.
When defining the variable "VPATH", use a space or colon (:) to separate multiple directories that need to be searched. The order of make search directories is in the order of the directories in the variable "VPATH" definition, and the current directory is always the first search directory. For example, the following definition

The dependent search directory is specified as the SRC directory under the current directory, and we can assign a value to the Vpath variable inside the makefile.rules, and give the current module the directory where the C file resides before including the Makefile.rules.
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 structure of the project directory, the corresponding dependent files are not in the same directory, and become troublesome, so it is better to directly assign values to the Vpath variable, we only need to specify the source of the directory is located.
In fact, we have another way of searching for file paths: Use the Vpath keyword (note not the Vpath variable), which is similar to Vpath, but it can specify a different search directory for different types of files (distinguished by file names). There are three methods of use:
1. Vpath PATTERN Directories
Specifies the search directory "directories" for all files that conform to the pattern "pattern". Multiple directories are separated using spaces or colons (:).
2. Vpath PATTERN
Clears the search path that was previously set for files that conform to pattern "pattern".
3, Vpath
Clears all file search paths that have been set.
The "pattern" in the Vapth usage method needs to contain the pattern character "%"; for example, the definition above:

VPATH + =/src  

Can be written as:

Vpath%.c./src  

Now give one of our makefile.rules:

        # Copyright (C) shallnew \at 163 \dot com # if without a platform defined, give value "un Know "to PLATFORM ifndef PLATFORM >---PLATFORM = unknow endif # define a          Root build directory base on the platform # If without a src_base defined, just use local SRC directory Ifeq ($ (src_base),) >---BUILDDIR = $ (mod_src_dir) >---objdir = $ (mod_src_dir) >---libd          IR = $ (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) >---libd    IR = $ (BUILDDIR)/lib      >---BINDIR = $ (BUILDDIR)/bin endif # Update compilation flags base on "Debug_symbols" Ifeq ($ (debug_symbols), TRUE) >---CFLAGS + =-g-wall-werror-o0 Else >---cflag S + =-wall-werror-o2 endif VPATH + $ (mod_src_dir) Src_obj = $ (patsubst%          . C, $ (objdir)/%.O, $ (Notdir $ (src_files))) ifeq ($ (Makelevel), 0) All:msg Else All:lib bin endif Lib: $ (objdir) $ (libdir)/$ (src_lib) Bin: $ (objdi                    R) $ (BINDIR)/$ (src_bin) $ (objdir): >---mkdir-p [email protected] Ifneq ($ (Src_bin),) $ (BINDIR)/$ (Src_bin): $ (Src_obj) >---$ (CC)-O [email protected] $^ $ (ldflag S) endif ifneq ($ (src_lib),) $ (libdir)/$ (src_lib): $ (Src_obj) >---$ (AR) RCS [EMAIL&NBSP;PROTECTED] $^ >---CP [email protected] $ (src_base)/libs endif $ (src_obj): $ (objdir)/          %.O:%.c >---$ (CC)-C $ (CFLAGS) $ (cppflags) $<-o [email protected] msg: >[email protected] "Cannot directily execute this makefile!                              This Makefile should called by TopLevel Makefile. "          # clean Target clean:ifneq ($ (src_lib),) >--->---$ (RM) $ (src_obj) $ (libdir)/$ (src_lib)                    endif ifneq ($ (src_bin),) >--->---$ (RM) $ (src_obj) $ (BINDIR)/$ (src_bin) endif .      Phony:all Clean

makefile--Catalog Search (eight)

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.