The Patsubst of Makefile

Source: Internet
Author: User

Often to write the project's makefile, or look at other items of the legacy project makefile, some makefile built-in functions commonly used, but used to forget, the recent project using PATSUBST, feel very useful format:$ (patsubst <pattern>,<replacement>,<text>)

Name: pattern string substitution function--patsubst.
Function: Find words in <text> (the words are separated by "space", "tab" or "carriage return") conform to the pattern <pattern>, and if so, replace with <replacement>.

Here,<pattern> can include the wildcard "%", which represents any length of string. If <replacement> also contains "%", then the "%" in the,<replacement> will be the string represented by the "%" in <pattern>.

(Can be escaped with "\", "\%" to indicate the true meaning of the "%" character)
Return: function returns the string that was replaced.

Example:

$ (Patsubst%.c,%.o, A.C B.C)

Replace the word string "A.C B.C" with [%.c] with [%.O] and return the result "A.O b.o"

There is a variable substitution reference in make

For a variable that has already been defined, you can use replace reference to replace the suffix character (string) in its value with the specified characters (strings). The format is "$ (var:a=b)" (or "${var:a=b}"),

This means replacing the word "B" at the end of all "A" characters in the variable "VAR". The meaning of "end" is preceded by a space (the variable value is separated by a space between multiple words). The "A" character for other parts of the variable is not replaced.

For example:

Foo: = A.O b.o C.O

Bar: = $ (FOO:.O=.C)

In this definition, the value of the variable "bar" is "A.C B.C c.c". Use the variable's replacement reference to replace the variable "foo" with the trailing character "O" of all words in a space-separated value with "C", with the rest remaining unchanged.

If "o.o" is present in the variable "foo", then the value of the variable "bar" is "A.C B.C c.c o.c" instead of "A.C B.C c.c c.c".



Example:Use the following 3 built-in functions 1, wildcard: extending wildcard characters
2. Notdir: Remove Path
3. PATSUBST: Replace wildcard characters
Set up a test directory to create a subdirectory named sub in the test directory
$ mkdir MK
$ CD MK
$ mkdir Sub

Under MK, establish A.C and B.C 2 files, in sub directory, build aa.c and BB.C 2 files

To build a simple makefile
src=$ (wildcard *.c./sub/*.c)
file=$ (Notdir $ (src))
obj=$ (Patsubst%.c,%.o,$ (SRC))
All
@echo $ (SRC)
@echo $ (file)
@echo $ (obj)


Execution Result Analysis:
First line output:
A.C B.C./sub/aa.c./SUB/BB.C

Wildcard all files that have the suffix C under the specified directory./and./sub/are expanded.

The second line of output:
A.C B.C aa.c BB.C
Notdir the expanded file to get rid of the path information

The third line of output:
A.O B.O AA.O BB.O

In $ (patsubst%.c,%.o,$ (SRC)), PATSUBST replaces the variable in $ (file) with the suffix of. c. O,
Any output.
Or you can use
obj=$ (FILE:%.C=%.O)
The effect is the same.

The Patsubst of Makefile

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.