A makefile that can automatically search for source files and automatically derive them.

Source: Internet
Author: User

today saw a day of makefile writing, patchwork, finally wrote a makefile, quite a sense of accomplishment, recorded, for review.

Suppose there are two header file directories header1,header2; Two CPP files directory, SRC1,SRC2, one lib directory

Write a complete makefile step as follows:

1 define the name of the executable file

TARGET = Main

2 Specifies the directory where the header file to be used by this compilation project is located

INC =-iheader1-iheader2

3 specify the directory where the CPP file resides

VPATH =.: Src1:src2

4 Specify all CPP files

Src_path = $ (foreachdir,$ (subst:, $ (VPATH)), $ (wildcard $ (dir)/*.cpp))

Explanation:

foreach , subst, wildcard is a built-in function for makefile, where the wildcard function is a string connection function that joins two parameters in a function into one . subst is a string substitution function in which the meaning is to replace the colon in the Vpath variable with a space. The Foreach function, which is the return value of the SUBST function, is delimited by a space, the loop value is assigned to dir, and then the wildcard is accepted . of the processing .

the general meaning of this sentence is: get each directory from Vpath, and find all the CPP files in this directory, assign to the Src_path variable.

5 Defines the directory where the intermediate object file is stored.

Obj_dir =./obj

6 All object files are stored in the directory corresponding to the source code, and the directory where the object file is defined

Obj_path = $ (addprefix $ (obj_dir)/, $ (subst:,, $ (VPATH) ))

Explanation:

Addprefix A function is a prefix -prefixed function that adds a prefix to all defined directories in Vpath ./obj/ .

7 Defines the name of all the object files that are required to connect to the destination file

OBJS = $ (Addprefix $ (Obj_dir), $ (subst.cpp,.o,$ (Src_path)))

8 Create a directory for an object file

Make_object_dir: = $ (Shell mkdir-p$ (obj_dir) $ (Obj_path))

*************************

9 defining a useful compiler

CXX = g++

Cxxflags = g

Ten define a build rule for the target file

$ (TARGET) : $ (OBJS)

$ (CXX)-o [email protected] $ (OBJS) $ (INC) $ (cxxflags)

One define How the object file is generated:

$ (Obj_dir)/%.O:%.cpp%. CPP.D

$ (CXX)-o [email protected]-C $< $ (INC) $ (cxxflags)

A define how the. cpp.d file is generated

DEPS = $ (OBJS:.O=.CPP.D)

$ (DEPS): $ (obj_dir)/%.CPP.D :%.cpp

$ (CXX) $<-mm $ (INC) >$ (obj_dir)/$<.d

- Defining cleanup Functions

Clean

@rm-RF./obj

@rm-RF $ (TARGET)

At this point , a generic makefile is completed, if you add the source code directory and header file directory, you only need to add this directory to the INC variable and the Vpath variable. automatic dependency generation of intermediate object files and files .

A makefile that can automatically search for source files and automatically derive them.

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.