Lesson 5 make makefile (Supplement)

Source: Internet
Author: User
Preface:

Some basic knowledge of makefile has been explained in the previous lessons and some small examples have been used in practice. Now, normal exercises are no problem. However, if there are many project files and hierarchical files, there will still be many problems. This lesson provides some additional knowledge.

Knowledge point:

1. Embedded functions of makefile;

2. Write the level-2 Directory;

3. Write multi-level directories;

Principle:

GNU make also provides functions to help us compile this file, making it more flexible and robust when writing makefile files. The provided functions are roughly divided into four types: processing file names, text content, variables, and commands; in this section, we won't talk too much about functions, or even the most basic function of removing spaces, sorting, and filtering. We only need a few functions. With these functions, we can easily handle compilation of level-2 Multi-Level directory files.

The Make function describes several common functions.

Practice:

1. Embedded Functions

1.1 The call format of the function is similar to the variable reference starting with $. The details are as follows:

$ (Function arguments) # It can also be: $ {function arguments}

Note that the function is the name of the function embedded in make. If the function is called indirectly by the user, arguments is separated by multiple parameters of the function, therefore, the parameters cannot be separated. Otherwise, an error occurs.

1.2 common

1.2.1 $ (wildcard pattern): Lists matching files and folders in the current directory. For example, it lists all target. O files in the current directory: SRC =$ (wildcard *. O ).

1.2.2 $ (patsubst pattern, replacement, text): replace the statements that conform to the pattern in text with replacement, for example, export Calc. o, calcmain. o its Calc. C and calcmain. c:

 

Objects = Calc. O calcmain. o $ (patsubst %. O, %. C, $ (objects) # (Objects:. O =. c) can also be used

 

1.2.3 $ (shell command): execute shell command, for example, execute to list all folders in the current directory:

 

$(shell ls -d */)

 

1.2.4 $ (findstring find, in): Find the find character in the in string. If so, return find; otherwise, return null;

 

$ (Findstring A, ABCD) # A is returned

 

2. makefile supports level-2 Directories

2.1 create an environment first. See the following code:

 

Mkdir gcc04cd gcc04mkdir UI # interface code library mkdir Dal # data access library mkdir BLL # business logic library CD uitouch UI. c UI. HCd .. /daltouch Dal. c Dal. HCd .. /blltouch BLL. c BLL. h

 

Write some simple test code for the UI, Dal, and BLL modules:

 

# UI. c file # include <stdio. h> # include "UI. H "Void print_ui (const char * Str) {printf (STR) ;}# UI. hfile # ifndef _ ui_h # DEFINE _ ui_hvoid print_ui (const char * Str); # endif # Dal. c file # include <stdio. h> # include "Dal. H "Void print_dal (const char * Str) {printf (STR) ;}# Dal. hfile # ifndef _ dal_h # DEFINE _ dal_hvoid print_dal (const char * Str); # endif # BLL. c file # include <stdio. h> # include "BLL. H "Void print_bll (const char * Str) {printf (STR) ;}# BLL. hfile # ifndef _ bll_h # DEFINE _ bll_hvoid print_bll (const char * Str); # endif

Let's take a look at the MAKEFILE file:

CC=gccCFLAGS=-Wall -gBIN=mainSUBDIR=$(shell ls -d */)ROOTSRC=$(wildcard *.c)ROOTOBJ=$(ROOTSRC:%.c=%.o)SUBSRC=$(shell find $(SUBDIR) -name ‘*.c‘)SUBOBJ=$(SUBSRC:%.c=%.o)$(BIN):$(ROOTOBJ) $(SUBOBJ)    $(CC) $(CFLAGS) -o $(BIN) $(ROOTOBJ) $(SUBOBJ).c.o:    $(CC) $(CFLAGS) -c $< -o [email protected]clean:    rm -f $(BIN) $(ROOTOBJ) $(SUBOBJ)

The MAKEFILE file written by the above several functions and derivation has been parsed accordingly as to its meaning.

Subdir variable also note that it is the set of directories under the current directory. Use the find command to find these. c files under the 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.