Write the project makefile from scratch (7): unify the target output directory

Source: Internet
Author: User
[Copyright statement: reprinted. Please retain the Source: blog.csdn.net/gentleliu. Mail: shallnew at 163 dot com]
In the previous section, we extracted the Rules separately to facilitate the maintenance of makefile. Each module only needs to provide some variables about itself, and then use the unified rule makefile. In this section, we continue to improve our makefile. So far, the output targets of the makefile compilation link are all in the same directory of the source file or the same directory of the module makefile, when a project is large, it will appear messy and it is difficult to find the file for compiling and output. Since makefile itself compiles the link program according to our rules, we can specify the directory of the compilation link target. In this way, we can clearly understand the output file, in addition, you can directly Delete the specified directory when clearing the compiled target. You do not need to enter the source code directory layer by layer to delete the directory, which improves the efficiency.
To unify the target output directory, the directory must exist. Therefore, we can add a rule to create these directories, including the directory for creating executable files, the directory for linking library files, and. o file directory. In addition, the directory can be used to determine whether to generate debugging information to separate the target file. Generally, the top-level directory of a project contains a build directory to store the results of the compiled target file. Currently, the build directory created through makefile in my project directory is as follows:
Build // build root directory ├ ── UNIX // The output directory without debugging information under the UNIX platform project │ ── bin // store the executable file directory │ ── lib // store file directory │ ── OBJ // storage. o file directory, which is generated by each module. o file directory │ ├ ── IPC │ ├ ── main │ ── tools ── unix_dbg // The debugging information output directory ── bin ── lib └ under the UNIX platform project ── OBJ ├ ── IPC ├ ── main └ ── tools14 directories, 0 files

The bin and Lib directories in the preceding directories are created in the top-level makefile, and OBJ and Its modules and subdirectories are created in the makefile of each module.
The directory for creating the top-level makefile is as follows:
ifeq ($(DEBUG_SYMBOLS), TRUE)>---BUILDDIR = ./build/$(PLATFORM)_dbgelse>---BUILDDIR = ./build/$(PLATFORM)endifall : $(BUILDDIR) $(MODULES)$(BUILDDIR):>[email protected] "    Create directory [email protected] ...">---mkdir -p $(BUILDDIR)/bin $(BUILDDIR)/lib
We have added the dependency target builddir to the All target. The rule for this target is to create the bin directory and lib directory. In this way, a directory will be created before each compilation.

Makefile in each module creates a directory for generating the. o file, as shown in the preceding directory tree. Similar to the top-level makefile, The makefile in each module must generate the desired directory name based on the platform, compilation and debugging information, and module name, and then add rules for creating the directory. Because each module performs these operations, we will write this part in the Rule makefile (makefile. Rule) as follows:
……# 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……ifeq ($(MAKELEVEL), 0)all : msgelseall : lib binendiflib : $(OBJDIR) $(LIBDIR)/$(SRC_LIB)bin : $(OBJDIR) $(BINDIR)/$(SRC_BIN)                                                                                                                       $(OBJDIR) :>[email protected] "   MKDIR $(notdir [email protected])...">[email protected] -p [email protected]……
Compile and view the build directory:
build/└── unix_dbg    ├── bin    ├── lib    └── obj        ├── ipc        ├── main        └── tools7 directories, 0 files 
Because debugging information is enabled, the unix_dbg directory is created and the bin, Lib, OBJ, and module directories are created under the directory. However, no files are stored in the directory.
So far, this section only describes how to create a unified target file storage directory. However, it is not complete to automatically generate the compiled target file to these directories. In fact, we only need to add paths to the target, but there are still some details to be processed. The specific makefile will not be provided in the next section.

Write the project makefile from scratch (7): unify the target output directory

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.