Write Makefile with me (Nine)

zhaozj2021-02-08  307

Use functions ----

You can use a function to process variables in makefile, so that our commands or rules are more flexible and intelligent. The function supported by Make is not much, but it is enough for our operation. After the function is called, the return value of the function can be used as a variable.

First, the function of the function call grammar

The function call, very like the use of variables, is also identified by "$", its syntax is as follows:

$ ( )

Or

$ { }

Here, is a function name, and the function supported by Make is not much. is the parameter of the function, the parameters are brought by a comma, and the function names and parameters are separated by "space". The function is called "$", enclosing the function name and parameters in parentheses or curly brackets. It feels like a variable, is it? The parameters in the function can use variables. For the unity of styles, the function and variables are best, such as "$ (Subst A, B, $ (x))", rather than "subs", B, $ {x}) "form. Because the unity will be clearer, it will also reduce some unnecessary trouble.

Still come and see an example:

Comma: =, EMPTY: = Space: = $ (EMPTY) $ (EMPTY) foo: = a b c bar: = $ (Subst $ (Space), $ (Comma), $ (Foo))

In this example, the value of $ (COMMA) is a comma. $ (space) Download a space, $ (foo) value is "ABC", $ (bar) defined, calling the function "subst", this is a replacement function, this function has three Parameters, the first parameter is replaced by the string, the second parameter is replacing the string, the third parameter is a string of the operation. This function is also replacing the space in $ (foo) into a comma, so the value of $ (bar) is "A, B, C".

Second, string processing functions

$ (Subst , , )

Name: String Replacement Function - Subst. Function: Replace the string in the string to . Returns: The function returns a string that is replaced.

Example: $ (Subst Ee, EE, Feet on The Street), replacing "EE" in "Feet on The Street" into "EE", the return result is "Feet on the street".

$ (Patsubst , , )

Name: Mode string replacement function - Patsubst. Function: Find words in (word "space", "tab" or "Enter", "Separation", if you match the mode , if you match, you will replace it in . Here, can include a wildcard "%", representing a string of any length. If contains "%", then this "%" in will be the string represented by the "%" in . (You can use "/" to escape, "/%" to indicate the "%" character) of the true meaning back: the function returns a string that is replaced. Example:

$ (PATSUBST% .c,%. O, X.c.c bar.c)

Replace the string "x.c.c bar.c" Comply with the word [%. C] to replace [% .o], return the result is "x.c.o bar.o"

Remarks:

This is a bit similar to the relevant knowledge that we have said in front of the "variable chapter". Such as: "$ (var: = )" Patsubst , , $ (VAR) ", and" $ ($ (VAR: = ) "It is equivalent to" $ (PatSubst% ,% , $ (var)). For example, it is: Objects = foo.o bar.o baz.o, then "$ (Objects: .o = .c)" and "patrsubst% .O,%. C, $ (objects))" is the same of.

$ (Strip )

Name: Go to Net Gift Function - Strip. Function: Remove the empty characters that start and end in the string. Returns: Returns the string value of the removed space. Example: $ (Strip A B C)

Take the string "a b c" to the beginning and end space, the result is "a b c".

$ (Findstring , )

Name: Find string functions - Findstring. Function: Find strings in the string . Returns: If you find it, return , otherwise you return an empty string. Example:

$ (FindString A, A B C) $ (Findstring A, B C)

The first function returns "A" string, the second return "" string (empty string)

$ (Filter , )

Name: Filter function - Filter. Function: Filter the words in the string in mode, keep the words that match the model . There can be multiple modes. Returns: Returns a string that conforms to mode . Example:

Sourcees: = foo.c bar.c baz.s Ugh.h foo: $ (Sources) CC $ (Filter% .C%. S, $ (Sources)) -O Foo $ (Filter% .C% .s, The value returned by $ (SOURCES) is "foo.c bar.c baz.s".

$ (Filter-Out , )

Name: Reverse filter function - Filter-out. Function: Filter the words in the string in mode, remove words that match the model . There can be multiple modes. Returns: Returns a string that does not match the mode . Example:

Objects = main1.o foo.o main2.o bar.o mains = main1.o main2.o $ (Filter-Out $ (MAINS), $ (Objects)) The return value is "foo.o bar.o". $ (Sort )

Name: Sort Function - Sort. Function: Sort by words in string (ascending). Returns: Returns the sorted string. Example: $ (Sort Foo Bar LOSE) Returns "Bar Foo Lose". Note: The Sort function removes the same words in .

$ (Word , )

Name: Take a word function - Word. Function: Take the string words in . (From one start) Returns: Returns "N> words in string . If is larger than the number of words in , the empty string is returned. Example: $ (Word 2, Foo Bar Baz) Return value is "bar".

$ (WordList , , )

Name: Take a string string function --wordList. Function: Take the word string from to from the string . and are a number. Returns: Returns the word string from to in string . If is larger than the number of words in , the empty string is returned. If is greater than the word number of , then return from start, to ends. Example: $ (WordList 2, 3, Foo Bar Baz) Return value is "bar baz".

$ (Words )

Name: Word Number Statistics - Words. Function: Counting the number of words in strings in . Returns: Returns the number of words in . Example: $ (Words, Foo Bar Baz) Return value is "3". Note: If we want to get the last word in , we can do this: $ (Word $ (Words ), ).

$ (Firstword )

Name: First single word function --firstword. Function: Take the first word in string . Returns: Returns the first word of string . Example: $ (FIRSTWORD FOO BAR) The return value is "foo". Remarks: This function can be implemented with a Word function: $ (Word 1, ). The above is all string operation functions, and if mixed, you can complete a more complex function. Here, an example of a reality is given. We know that make uses the "vPath" variable to specify the search path of "dependent file". Thus, we can use this search path to specify the search path parameter cflags for the compiler to the header file, such as:

Override cflags = $ (PatSubst%, - I%, $ (Subst:,, $ (vPath)))

If our "$ (vPath) value is" src: ../ Headers "," $ (PatSubst%, - I%, $ (Subst:,, $ (vPath)) "will return" -isrc - I ../ Headers, this is the parameter of the CC or GCC search header file path.

Third, file name operation function

Below we want to introduce the function to process the file name. The parameter string of each function will be treated as one or a series of file names.

$ (DIR )

Name: Take a directory function - DIR. Function: Take out the directory section from the file name sequence . The directory section refers to the part before the last backslash ("/"). If there is no backslash, then "back" ./ ". Returns: Returns the directory section of the file name sequence . Example: $ (Dir src / foo.c Hacks) The return value is "src / ./".

$ (notdir )

Name: Take the file function - NOTDIR. Function: Take an unlibular part from the file name sequence . The non-directory part refers to the part after the last backslash ("/"). Returns: Returns the non-directory part of the file name sequence . Example: $ (notdir src / foo.c Hacks) Return value is "foo.c Hacks". $ (Suffix ) Name: Take the suffix function - Suffix. Function: Take out the suffix of each file name from the file name sequence . Returns: Returns the suffix sequence of the file name sequence , if the file has no suffix, return the empty string. Example: $ (SUFFIX SRC / FOO.c SRC-1.0 / bar.c Hacks) Return Value ".c .c".

$ (BaseName )

Name: Take the prefix function - Basename. Function: Take out the prefix section of each file name from the file name sequence . Returns: Returns the prefix sequence of the file name sequence , if the file is not prefix, return the empty string. Example: $ (BaseName Src / Foo.c SRC-1.0 / bar.c Hacks) Return Value is "SRC / FOO SRC-1.0 / Bar Hacks".

$ (Addsuffix , )

Name: Add the assales - ADDSUFFIX. Function: Adding the suffix to each word in . Returns: Returns the file name sequence of the plus suffix. Example: $ (addsuffix .c, foo bar) Return value is "foo.c bar.c". $ (Addprefix , )

Name: Add the prefix function - ADDPREFIX. Function: Adding the prefix to each word in . Returns: Returns the file name sequence of the added prefix. Example: $ (AddPRefix SRC /, FOO BAR) Return Value is "SRC / Foo Src / Bar".

$ (Join , )

Name: Connection function - Join. Function: Add the words in to the words of . If the number of words of is more than , the number of words in will remain in the original. If the word number of is more than , the word "list" will be copied to . Returns: Returns the connected string. Example: $ (Join AAA BBB, 111 222 333) Return value is "AAA111 BBB222 333".

<- Previous Next -> (All rights reserved, please indicate the author and source)

转载请注明原文地址:https://www.9cbs.com/read-1121.html

New Post(0)