Table 1-6 common command line options of the GNU make command
Definition of command line options
-C dir changes to the specified directory DIR before reading makefile.
-F FILE uses the specified FILE as the makefile.
-H: all make options are displayed.
-I ignore all command execution errors.
-When I DIR contains other makefile files, you can use this option to specify the search directory.
-N: only the commands to be executed are printed, but these commands are not executed.
-P: displays the make variable database and implicit rules.
-S does not display commands when executing commands.
-W displays the working directory before and after makefile processing.
-W file: assume that the FILE has been modified.
Table 1-5 main pre-defined variables of GNU make
Predefine variable meaning
$ * The name of the target file that does not contain the extension.
$ + All dependent files are separated by spaces and appear sequentially, which may contain repeated dependent files.
$ <Name of the first dependent file.
$? All dependent files are separated by spaces. The modified date of these dependent files is later than the object creation date.
$ @ Full name of the target.
$ ^ All dependent files are separated by spaces and do not contain duplicate dependent files.
$ % If the target is an archive member, this variable indicates the name of the target archive member. For example, if the target name is mytarget. so (image. o), $ @ is mytarget. so, and $ % is image. o.
Automatic Variable meaning
The name of the AR archive maintenance program. The default value is ar.
ARFLAGS archive maintenance program options.
The name of the AS assembler. The default value is.
ASFLAGS assembler options.
The name of the cc c compiler. The default value is cc.
Ccflags c compiler options.
Name of the cpp c pre-compiler. The default value is $ (CC)-E.
Cppflags c pre-compiled options.
The name of the cxx c ++ compiler. The default value is g ++.
Cxxflags c ++ compiler options.
The name of the fc fortran compiler. The default value is f77.
Fflags fortran compiler options.
Bytes ----------------------------------------------------------------------------------------------------------------------
Use Functions
----
In Makefile, functions can be used to process variables, making our commands or rules more flexible and intelligent. Make does not support many functions, but it is enough for us to operate. After a function is called, the return value of the function can be used as a variable.
I. function call syntax
Function calling is similar to the use of variables and identified by "$". Its syntax is as follows:
$ (<Function >;< arguments> ;)
Or
$ {<Function >;< arguments> ;}
Here, <function>; is the function name. make does not support many functions. <Arguments>; is a function parameter. parameters are separated by commas (,), and function names and parameters are separated by spaces. Function calls start with "$" and enclose function names and parameters in parentheses or curly brackets. It feels like a variable, isn't it? Function parameters can be changed
For the sake of style unification, the function and the variable should have the same brackets, such as using the form of "$ (subst a, B, $ (x, instead of "$ (subst a, B, $ {x. Because unification will be clearer and reduce unnecessary troubles.
Let's look at an example:
Comma: =,
Empty: =
Space: = $ (empty)
Foo: = a B c
Bar: =$ (subst $ (space), $ (comma), $ (foo ))
In this example, $ (comma) is a comma. $ (Space) defines a space using $ (empty). The value of $ (foo) is "a B c", and $ (bar) is used for definition, the "subst" function is called. This is a replacement function. This function has three parameters. The first parameter is the replaced string, the second parameter is the replaced string, and the third parameter is the replaced string.
The operation string. This function replaces spaces in $ (foo) with commas, so the value of $ (bar) is "a, B, c ".
Ii. string processing functions
$ (Subst <from >;,< to >;,< text> ;)
Name: String replacement function-subst.
Function: replace <from>; In the <text>; string with <to> ;.
Return: The function returns the replaced string.
Example:
$ (Subst ee, EE, feet on the street ),
Replace "ee" in "feet on the street" with "EE" and return "fEEt on the strEEt ".
$ (Patsubst <pattern >;,< replacement >;,< text> ;)
Name: patsubst, a mode string replacement function.
Function: query whether the matching mode is true for words in <text>; (the words are separated by "space", "Tab", or "Press ENTER" and "line feed, replace with <replacement>. Here, <pattern>; can include the wildcard "%" to indicate strings of any length. If <replacement>; also contains "%", then,
<Replacement>; the "%" in will be the string represented by the "%" in <pattern>. (Escape with "/" and use "/%" to indicate the "%" character of the true meaning)
Return: The function returns the replaced string.
Example:
$ (Patsubst %. c, %. o, x. c. c bar. c)
Returns the string "x. c. c bar. c "Conformity mode [%. c] to [%. o], the returned result is "x. c. o bar. o"
Note:
This is a bit similar to the knowledge we have mentioned in the previous "variable chapter. For example:
"$ (Var: <pattern >;=< replacement> ;)"
Equivalent
"$ (Patsubst <pattern >;,< replacement >;,$ (var ))",
And "$ (var: <suffix >;=< replacement> ;)"
Is equivalent
"$ (Patsubst % <suffix >;,%< replacement >;,$ (var ))".
For example, objects = foo. o bar. o baz. o,
Then, "$ (objects:. o =. c)" and "$ (patsubst %. o, %. c, $ (objects)" are the same.
$ (Strip <string> ;)
Name: Remove space function -- strip.
Function: removes the <string>; empty characters at the beginning and end of the string.
Return: returns the string value with spaces removed.
Example:
$ (Strip a B c)
Place the string "a B c" to the space at the beginning and end, and the result is "a B c ".
$ (Findstring <find >;,< in> ;)
Name: search for the string function-findstring.
Function: find the <find>; string in the <in>; string.
Return Value: If yes, <find>; is returned. Otherwise, an empty string is returned.
Example:
$ (Findstring a, a B c)
$ (Findstring a, B c)
The first function returns the "a" string, and the second returns the "" string (Null String)
$ (Filter <pattern... >;,< text> ;)
Name: filter function.
Function: Filter <text> in <pattern>; the words in the string, retain the words that match the pattern <pattern>. There can be multiple modes.
Return: returns the string that matches the pattern <pattern>.
Example:
Sources: = foo. c bar. c baz. s ugh. h
Foo: $ (sources)
Cc $ (filter %. c %. s, $ (sources)-o foo
$ (Filter %. c %. s, $ (sources) returns "foo. c bar. c baz. s ".
$ (Filter-out <pattern...>;, <text> ;)
Name: filter-out.
Function: Filter <text>; strings in the <pattern>; mode to remove words in the <pattern>; mode. There can be multiple modes.
Return: returns a string that does not conform to the pattern <pattern>.
Example:
Objects = main1.o foo. o main2.o bar. o
Mains = main1.o main2.o
$ (Filter-out $ (mains), $ (objects) returns "foo. o bar. o ".
$ (Sort <list> ;)
Name: Sorting function -- sort.
Function: sorts the words in string <list>; in ascending order ).
Return: returns the sorted string.
Example: $ (sort foo bar lose) returns "bar foo lose ".
Note: The sort function removes the same words from <list>.
$ (Word <n >;,< text> ;)
Name: word function -- word.
Function: obtains the <text>; nth> word in the string. (From the beginning)
Return: the number of <n> In the <text>; string is returned. If <n>; is greater than the number of words in <text>;, an empty string is returned.
Example: $ (word 2, foo bar baz) returns "bar ".
$ (Wordlist <s >;,< e >;,< text> ;)
Name: wordlist, a single string function.
Function: obtains the word string from <s>; to <e>. <S>; and <e>; are numbers.
Return: return the word string from <s>; to <e>; in <text>. If <s>; is greater than the number of words in <text>;, an empty string is returned. If the number of words <e>; greater than <text>;, the string starting from <s>; and ending from <text>; is returned.
Example: $ (wordlist 2, 3, foo bar baz) returns "bar baz ".
$ (Words <text> ;)
Name: Word Count statistical function -- words.
Function: counts the number of words in a string.
Return: returns the number of words in <text>.
Example: $ (words, foo bar baz) returns "3 ".
Note: if we want to take the last word in <text>;, we can: $ (word $ (words <text>;), <text> ;).
$ (Firstword <text> ;)
Name: the first word function, firstword.
Function: gets the first word in the <text>; string.
Return: returns the first word of the string <text>.
Example: $ (firstword foo bar) returns "foo ".
Note: this function can be implemented using the word function: $ (word 1, <text> ;).
The above are all string operation functions. If used in combination, you can complete more complex functions. Here is an example of a real application. We know that make uses the "VPATH" variable to specify the search path for "dependent Files. Therefore, we can use this search path to specify the search path parameter CFLAGS of the compiler's first-class file, as shown in
:
Override CFLAGS + = $ (patsubst %,-I %, $ (subst:, $ (VPATH )))
If our "$ (VPATH)" value is "src :.. /headers ", then" $ (patsubst %,-I %, $ (subst:, $ (VPATH) "will return"-Isrc-I .. /headers ", which is the parameter of the header file path for cc or gcc search.
Iii. File Name operation functions
The functions we will introduce below mainly process file names. The parameter strings of each function are treated as one or a series of file names.
$ (Dir <names...> ;)
Name: Directory function -- dir.
Function: extracts the directory from the file name sequence <names>. The directory part refers to the part before the last backslash. If there is no backslash, "./" is returned.
Return: returns the directory part of the file name sequence <names>.
Example: $ (dir src/foo. c hacks) returns "src /./".
$ (Notdir <names...> ;)
Name: file function -- notdir.
Function: Extracts non-directory parts from the file name sequence <names>. The non-directory part is the part after the last backslash.
Return: the non-directory part of the file name sequence <names>.
Example: $ (notdir src/foo. c hacks) returns "foo. c hacks ".
$ (Suffix <names...> ;)
Name: suffix, A suffix function.
Function: extracts the suffix of each file name from the file name sequence <names>.
Return: The suffix sequence of the file name sequence <names>;. If the file does not have a suffix, an empty string is returned.
Example: $ (suffix src/foo. c src-1.0/bar. c hacks) returns ". c. c ".
$ (Basename <names...> ;)
Name: prefix function -- basename.
Function: extracts the prefix of each file name from the file name sequence <names>.
Return: the prefix sequence of the file name sequence <names>;. If the file does not have a prefix, an empty string is returned.
Example: $ (basename src/foo. c src-1.0/bar. c hacks) returns "src/foo src-1.0/bar hacks ".
$ (Addsuffix <suffix>;, <names...> ;)
Name: add the suffix function addsuffix.
Function: adds the suffix <suffix>; to the end of each word in <names>.
Return: returns the sequence of file names with suffixes.
Example: $ (addsuffix. c, foo bar) returns "foo. c bar. c ".
$ (Addprefix <prefix >;,< names...> ;)
Name: The addprefix function.
Function: adds the prefix <prefix>; to the end of each word in <names>.
Return: returns the sequence of file names that have been prefixed.
Example: $ (addprefix src/, foo bar) returns "src/foo src/bar ".
$ (Join <list1 >;,< list2> ;)
Name: join function -- join.
Function: append the word in <list2>; to the word in <list1>. If <list1>; has more words than <list2>;, the multiple words in <list1>; remain unchanged. If <list2>; has more words than <list1>;, <list2>; multiple words are copied to <list2>.
Return: returns the connected string.
Example: $ (join aaa bbb, 111 222 333) returns "aaa111 bbb222 333 ".
4. foreach Functions
The foreach function is very different from other functions. Because this function is used for loop purposes, the foreach function in Makefile is almost the same as the for statement in the Unix standard Shell (/bin/sh, or a foreach statement in C-Shell (/bin/csh. Its syntax is:
$ (Foreach <var >;,< list >;,< text> ;)
This function is used to extract the words in the <list>; parameter one by one and put them into the parameter <var>; the specified variable, and then execute the expressions contained in <text>. Each <text>; returns a string. During the loop, each string returned by <text>; is separated by spaces. When the loop ends, <text>; each word returned
The entire string consisting of a string (separated by spaces) will be the return value of the foreach function.
Therefore, <var>; preferably a variable name, <list>; can be an expression, and <text>; is generally used in <var>; this parameter is used to enumerate the words in <list>. For example:
Names: = a B c d
Files: = $ (foreach n, $ (names), $ (n). o)
In the above example, the words in $ (name) are extracted one by one and coexist in the variable "n", "$ (n ). o "calculates a value based on" $ (n) "each time. These values are separated by spaces and finally returned as the foreach function. Therefore, the value of $ (files) is". o B. o c. o d. o ".
Note: The <var>; parameter in foreach is a temporary local variable. After the foreach function is executed, the variable of the <var>; parameter does not work, its scope is only in the foreach function.
V. if Functions
The if function is similar to the conditional statement supported by GNU make-ifeq (see the previous section). the syntax of the if function is:
$ (If <condition >;,< then-part> ;)
Or
$ (If <condition >;,< then-part >;,< else-part> ;)
It can be seen that the if function can contain "else" or not. That is, if function parameters can be two or three. <Condition>; the parameter is the if expression. if it returns a non-null string, the expression returns the true result. Therefore, <then-part>; is calculated, otherwise, <else-part>; is calculated.
The Return Value of the if function is: <then-part> if <condition>; true (non-null string), which is the return value of the entire function, if <condition>; if it is a false (Null String), <else-part>; is the return value of the entire function. If <else-part>; is not defined, the entire function returns an empty string.
Therefore, only one <then-part>; and <else-part> are calculated.
Vi. call Functions
The call function is the only parameterized function that can be created. You can write a very complex expression. In this expression, you can define many parameters, and then you can use the call function to pass parameters to this expression. Its syntax is:
$ (Call <expression >;,< parm1 >;,< parm2 >;,< parm3> ;...)
When make executes this function, variables in the <expression>; parameter, such as $ (1), $ (2), $ (3), will be <parm1> ;, <parm2>;, <parm3>; are replaced in sequence. The return value of <expression>; is the return value of the call function. For example:
Reverse = $ (1) $ (2)
Foo = $ (call reverse, a, B)
Then, the value of foo is "a B ". Of course, the order of parameters can be customized, not necessarily in order, for example:
Reverse = $ (2) $ (1)
Foo = $ (call reverse, a, B)
The value of foo is "B ".
VII. origin Functions
Unlike other functions, the origin function does not operate on the value of a variable. It just tells you where the variable comes from? Its syntax is:
$ (Origin <variable> ;)
Note: <variable>; is the variable name and should not be referenced. Therefore, you 'd better not use the "$" character in <variable>. The Origin function uses its return value to tell you the "Birth condition" of the variable. below, the return value of the origin function is:
"Undefined"
If <variable>; has never been defined, the origin function returns the value "undefined ".
"Default"
If <variable>; is a default definition, such as the "CC" variable, this variable will be described later.
"Environment"
If <variable>; is an environment variable and the Makefile is executed, the "-e" parameter is not enabled.
"File"
If <variable>; the variable is defined in Makefile.
"Command line"
If <variable>; the variable is defined by the command line.
"Override"
If <variable>; is redefined by the override indicator.
"Automatic"
If <variable>; is an automation variable in command running. Automation variables will be described later.
This information is very useful for compiling Makefile. For example, suppose we have a Makefile which contains a definition file Make. def, in Make. def defines a variable "bletch", and our environment also has an environment variable "bletch". At this time, we want to determine if the variable is from the environment, then we will take it seriously.
If it comes from a non-environment such as Make. def or command line, we will not redefine it. Therefore, in our Makefile, we can write as follows:
Ifdef bletch
Ifeq "$ (origin bletch)" "environment"
Bletch = barf, gag, etc.
Endif
Endif
Of course, you may say that you can't redefine the variables in the environment without using the override keyword? Why do we need to use this step? Yes, we can achieve this by using override, but override is too crude. It will also overwrite the variables defined from the command line, and we just want to redefine the environment, not heavy
The command line is newly defined.
VIII. shell functions
Shell functions are not like other functions. As the name suggests, its parameter should be the command of the operating system Shell. It is the same as the anti-quotation mark. This means that the shell function returns the output after the operating system command is executed. Therefore, we can use the operating system commands and string processing commands awk, sed, and so on to generate a variable
Quantity, such:
Contents: = $ (shell cat foo)
Files: = $ (shell echo *. c)
Note: This function will generate a new Shell program to execute commands, so you should pay attention to its running performance. If your Makefile contains some complicated rules and uses this function extensively, this is harmful to your system performance. In particular, the hidden rules of Makefile may make your shell function run more times than you think.
.
9. make control functions
Make provides some functions to control the running of make. Generally, you need to check the runtime information when running Makefile, and decide whether to let make continue or stop based on the information.
$ (Error <text...> ;)
Generate a fatal error, <text...>; is the error message. Note: The error function will not generate an error message when it is used. Therefore, if you define it in a variable and use this variable in subsequent scripts, that's fine. For example:
Example 1:
Ifdef ERROR_001
$ (Error is $(ERROR_001 ))
Endif
Example 2:
ERR = $ (error found an error !)
. PHONY: err
Err:; $ (ERR)
The example will generate an error call when the variable ERROR_001 is defined and then executed, while the Example 2 will generate an error call when the directory err is executed.
$ (Warning <text...> ;)
This function is similar to the error function, but it does not let make exit, but outputs a warning message, while make continues to execute.