Brief Introduction to wildcard notdir patsubst in makefile

Source: Internet
Author: User

1. wildcard: Extended wildcard
2. notdir: remove the path
3. patsubst: Replace the wildcard character

Example:
Create a test directory and create a subdirectory named sub under the test directory.
$ Mkdir Test
$ CD Test
$ Mkdir sub

Create a. C and B. C2 files under test, and create SA. C and SB. C2 files under sub directory.

Create a simple makefile
Src = $ (wildcard *. C./SUB/*. c)
Dir = $ (notdir $ (SRC ))
OBJ = $ (patsubst %. C, %. O, $ (DIR ))

ALL:
@ Echo $ (SRC)
@ Echo $ (DIR)
@ Echo $ (OBJ)
@ Echo "end"
 
Result Analysis:
First line output:
A. c B. C./SUB/SA. C./SUB/SB. c

Wildcard expands all files with the suffix C under the specified directory./And./SUB.

Output in the second row:
A. c B. C SA. c sb. c
Notdir removes the path information from the expanded File

Output in the third row:
A. o B. O SA. O Sb. o

In $ (patsubst %. C, %. O, $ (DIR), patsubst replaces all variables in $ (DIR) with. O whose suffix is. C,
Any output.
Or you can use
OBJ = $ (DIR: %. c = %. O)
The same is true.

Here, the replacement reference rule in makefile is used to replace another variable with the variable you specified.
Its standard format is
$ (VaR: A = B) or $ {var: a = B}
It means to replace a with B at the end of each value in the VaR variable.

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.