Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the purpose of the makefile keyword

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what is the role of the makefile keyword, the content is very detailed, interested friends can refer to, hope to be helpful to you.

I. the calling syntax of the function

Function calls, much like the use of variables, are also identified by "$" and their syntax is as follows:

$(;)

Or

${;;}

Here,; is the function name, and there are not many functions supported by make. ; is the parameter of the function, amusing each other

The sign "," is separated, and the function name and parameter are separated by a "space". The function call starts with "$" and encloses the function name and arguments in parentheses or curly braces. It feels like a variable, doesn't it? Parameters in a function

You can use string handling functions.

$(subst;,;)

Name: string replacement function? subst.

Function: replace the string in the string with;.

Return: the function returns the string that has been replaced.

Example:

$(subst ee,EE,feet on the street)

Put "feet on"

Replace "ee" with "EE" in "the street" and return "fEEt on the strEEt".

$(patsubst;,;)

Name: pattern string replacement function? patsubst.

Function: look up the words in (change the words with "space", "Tab" or "enter"

(line separation) whether it matches the pattern; if it matches, replace it with;. Here, you can include the wildcard "%", which represents a string of any length. If; also contains "%", then the "%" in; will be the string represented by the "%" in. (you can escape with "\" and use "\%".

The "%" character that represents the true meaning)

Return: the function returns the string that has been replaced.

Example:

$(patsubst% .c,% .o, x.c.c bar.c)

Replace the word "x.c.c bar.c" that matches the pattern [% .c] with [% .o], and the return result is "x.c.o bar.o"

Note:

This is somewhat similar to the relevant knowledge we mentioned in the previous "variables chapter". Such as:

"$(var:;=;)"

Equivalent to

"$(patsubst;, $(var))"

And "$(var:; =;)"

Is equivalent to

"$(patsubst;,%;, $(var))".

For example: objects = foo.o bar.o baz.o

Then "$(objects:.o=.c)" and "$(patsubst% .o,% .c, $(objects))" are the same.

$(strip;)

Name: go to the space function? strip.

Function: remove the empty characters at the beginning and end of the string.

Return: returns the string value of the stripped space.

Example:

$(strip a b c)

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

(findstring;,;)

Name: find string function? findstring.

Function: find the string in the string.

Return: if found, return;, otherwise return an empty string.

Example:

$(findstring a dint a b c)

$(findstring a _ r b c)

The first function returns the "a" string, and the second returns the "a" string (empty string)

(filter;,;)

Name: filter function? filter.

Function: filter the words in the string by; mode, and keep the words in accordance with the pattern. There can be multiple modes.

Return: returns a string that matches the pattern;.

Example:

Sources: = foo.c bar.c baz.s ugh.h

Foo: $(sources)

Cc $(filter% .c% .s, $(sources))-o foo

The value returned by $(filter% .c% .s, $(sources)) is "foo.c bar.c baz.s".

(filter-out;,;)

Name: anti-filter function? filter-out.

Function: filter the words in the string with the pattern, and remove the words that match the pattern. There can be multiple modes.

Return: returns a string that does not match the pattern;.

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: sorting function? sort.

Function: sort the words in the string (ascending order).

Return: returns the sorted string.

Example: $(sort foo bar lose) returns "bar foo lose".

Note: the sort function removes the same word from;.

(word;,;)

Name: take the word function? word.

Function: take the string; the first word in the middle. (from the beginning)

Return: returns the first word in the string. If the number of words in; is larger than that in;, an empty string is returned.

Example: the return value of $(word 2, foo bar baz) is "bar".

$(wordlist;,;)

Name: take the word string function? wordlist.

Function: a string of words taken from a string to a string. It's a number.

Return: return string; from; to

A string of words. If the number of words in; is larger than that in;, an empty string is returned. If; is greater than the number of words of;, then returns the word string that starts, ends, and ends.

Example: the return value of $(wordlist 2,3, foo bar baz) is "bar baz".

$(words;)

Name: word count function? words.

Function: count the number of words in the string.

Return: returns the number of words in;.

Example: the return value of $(words, foo bar baz) is "3".

Note: if we want to take the last word in;, we can go like this: $(word $(words;),;).

$(firstword;)

Name: the first word function? firstword.

Function: take the first word in the string.

Return: returns the first word of the string;.

Example: the return value of $(firstword foo bar) is "foo".

Note: this function can be implemented using the word function: $(word 1 word;).

Above, are all the string manipulation functions, if mixed with the use, you can complete more complex functions. Here, give an example of a real-world 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 compiler search path parameter CFLAGS for header files, such as:

Override CFLAGS + = $(patsubst%,-I% dai $(subst:, $(VPATH)

If our "$(VPATH)" value is "src:../headers", then "$"

(patsubst%,-I% dint $(subst:, $(VPATH) "will return"-Isrc-

I../headers ", which is the parameter for cc or gcc to search for the header file path.

3. File name operation function

The functions we will introduce below mainly deal with file names. The argument string of each function is treated as one or a series of file names.

$(dir;)

Name: take the directory function? dir.

Function: extract the directory part from the file name sequence. The directory section refers to the part before the last backslash ("/"). If there is no backslash, return ". /".

Return: returns the directory portion of the sequence of file names.

Example: the return value of $(dir src/foo.c hacks) is "src/. /"

$(notdir;)

Name: take the file function notdir.

Function: extract the non-directory part from the file name sequence. The non-directory part refers to the part after the last backslash ("/").

Return: returns the non-directory portion of the file name sequence.

Example: the return value of $(notdir src/foo.c hacks) is "foo.c hacks".

$(suffix;)

Name: suffix function? suffix.

Function: extract the suffixes of each file name from the file name sequence.

Return: returns the suffix sequence of the file name sequence, or an empty string if the file has no suffix.

Example: the return value of $(suffix src/foo.c src-1.0/bar.c hacks) is ".c.c".

$(basename;)

Name: take the prefix function basename.

Function: extract the prefix part of each file name from the file name sequence.

Return: returns the prefix sequence of the file name sequence, or an empty string if the file does not have a prefix.

Example: the return value of $(basename src/foo.c src-1.0/bar.c hacks) is "src/foo src-1.0/bar hacks".

(addsuffix;,;)

Name: suffix function? addsuffix.

Function: add a suffix to each word in.

Return: returns the suffixed sequence of file names.

Example: the return value of $(addsuffix. C, foo bar) is "foo.c bar.c".

(addprefix;,;)

Name: prefix function? addprefix.

Function: add a prefix to each word in.

Return: returns a prefixed sequence of file names.

Example: the return value of $(addprefix src/,foo bar) is "src/foo src/bar".

(join;,;)

Name: connection function? join.

Function: add the words in; correspondingly to the words in

behind. If the number of words in; is more than that in;, then the extra words in; will remain the original

Sample. If the number of words is higher than that of;, then the extra words will be copied to

; in.

Return: returns the concatenated string.

Example: the return value of $(join aaa bbb, 1112222333) is "aaa111 bbb222 333".

4. Foreach function

The foreach function is very different from other functions. Because this function is used for loops, the foreach function in Makefile is almost modeled on the Unix standard

Built from the for statement in Shell (/ bin/sh), or the foreach statement in C-Shell (/ bin/csh). Its syntax is:

$(foreach;,;)

This function means to take the words in the parameter; one by one into the parameter; in the specified variable and then execute it; the expression contained in it. Each time; will return a string, and during the loop, each string returned by; will

Separated by spaces, and finally, when the entire loop ends, the entire string (separated by spaces) of each string returned will be the return value of the foreach function.

So,; preferably a variable name,; can be an expression, and; is generally used in;; this parameter enumerates the words in; in turn. For example:

Names: = a b c d

Files: = $(foreach njue $(names), $(n) .o)

In the above example, the words in $(name) are taken out one by one and stored in the variable "n", and "$(n) .o" calculates a value based on "$(n)" each time, separated by spaces.

Interval, and finally as the return of the foreach function, so the value of $(files) is "a.o b.o c.o d.o".

Note that the; parameter in foreach is a temporary local variable, and after the foreach function is executed, the argument; will no longer function, and its scope is only in the foreach function.

5. If function

The if function is very similar to the conditional statement ifeq supported by GNU's make (see the previous section). The syntax of the if function is:

(if;,;)

Or

$(if;,;)

As you can see, the if function can contain the "else" section or not. That is, the parameters of the if function can be two or three. ; the parameter is

The expression of if, if it returns a non-empty string, is equivalent to returning true, so,; will be evaluated, otherwise; will be evaluated.

And the return value of the if function is, if; is true (not an empty string), that; will be the entire function

The return value, if; is false (empty string), then; will be the return value of the entire function, and if; is not defined, the entire function returns an empty string.

So,; and only one will be calculated.

6. Call function

The call function is the only function that can be used to create new parameterization. You can write a very complex expression in which you can define many parameters, and then you can use the call function to pass parameters to the expression. Its syntax is:

$(call;,;...)

When make executes this function, the variables in the parameters, such as $(1), $(2), $(3), etc., are replaced by the parameters. And the return value of

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, such as:

Reverse = $(2) $(1)

Foo = $(call reverse,a,b)

At this point, the value of foo is "b a".

7. Origin function

Unlike other functions, the origin function does not manipulate the value of a variable, it just tells you where your variable came from. Its syntax is:

$(origin;)

Note that; is the name of the variable, not a reference. So you'd better not use the "$" character in;. The Origin function will tell you the "birth status" of this variable with its return value. Here is the return value of the origin function:

"undefined"

If; has never been defined, the origin function returns the value "undefined".

"default"

If; is a default definition, such as the variable "CC", which we will talk about later.

"environment"

If; is an environment variable, and when Makefile is executed, the "- e" parameter is not turned on.

"file"

If; this variable is defined in Makefile.

"command line"

If; this variable is defined by the command line.

"override"

If; is redefined by the override indicator.

"automatic"

If; is an automatic variable in a command run. Automation variables will be discussed later.

This information is very useful for us to write Makefile. For example, suppose we have a Makefile that packages a definition file, Make.def, in

Make.def defines a variable "bletch", and we also have an environment variable "bletch" in our environment. At this point, we want to determine that if the variable comes from the environment, then

We redefine it, and if it comes from a non-environment such as Make.def or the command line, then we don't redefine it. So, in our Makefile, we can write:

Ifdef bletch

Ifeq "$(origin bletch)"environment"

Bletch = barf, gag, etc.

Endif

Endif

Of course, you might say, isn't it possible to redefine variables in the environment using the override keyword? Why do you need to use such a step? Yes, we can use override.

To achieve this effect, override is so rude that it overrides variables defined from the command line, and we just want to redefine what comes from the environment, not what comes from the command line.

Variables, in order to unify the style, the parentheses of functions and variables should be the same, such as using the form of "$(subst a dint bjue $(x))".

On what the role of the makefile keyword is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report