Pattern rules in the makefile

Source: Internet
Author: User

Pattern rules

Pattern rules are similar to normal rules. In the pattern rule only, the target name needs to contain a pattern character "%" (one), and a target containing the pattern character "%" is used to match a filename, and "%" can match any non-empty string. "%" can also be used in the dependent file of the rule, and the value of the pattern character "%" in the dependent file is determined by the "%" in the target. For example, for pattern rule "%.O:%.c", it means that all. o files depend on the corresponding. c file. We can use pattern rules to define implied rules.

Note that the matching and substitution of the pattern character "%" occurs after all the variable and function references in the rule are expanded. The expansion of variables and functions generally occurs when make reads Makefile (the expansion of variables and functions can refer to the function in chapter fifth using variables and chapter seventh make), while the "%" in pattern rules Matches and replacements occur when make is executed.

Introduction to Pattern Rules

In the pattern rule, the destination file is a file with the pattern character "%", and the pattern is used to match the target file. The pattern character "%" in the file name can match any non-empty string, except for the part of the pattern character that requires consistency. For example, "%.c" matches all files ending with ". C" (a minimum length of 3 letters for the matching filename), "s%.c" matches all the first letters "s" and must end with ". C" with a minimum filename length of 5 characters (the pattern character "%" matches at least one character). The "%" matching part of the target file name is called the "stem" (referred to in the 4.12 static mode section, as mentioned earlier). When a pattern rule is used, the target file is matched with a "stem" that relies on the "stem" to produce the corresponding dependent file, which must exist or be created.

Therefore, the format of a pattern rule is:

%.O:%.c; COMMAND ...

This pattern rule specifies how the file "N.C" is created to create the file "N.O", and the file "N.C" should either exist or be created.

The dependent file in the pattern rule can also not contain the pattern character "%". When the dependent file name does not contain the pattern character "%", it means that all target files that conform to the target schema depend on a specified file (for example:%.o:debug.h, which means that all. o files are dependent on the header file "Debug.h"). Such pattern rules are useful in many contexts.

The same pattern rule can have multiple targets. Multi-Objective pattern rules are somewhat different from common multiobjective rules, the processing of common multi-objective rules is to treat each goal as a separate rule, so multiple goals correspond to multiple independent rules (these rules have their own command line, the command line of each rule may be the same). But for the multi-target pattern rule, the goal of all rules is to have a command line that relies on files and rules, and the commands defined by the rule are likely to execute when the file conforms to any of the multiple target modes, because multiple destinations share the command line of the rule, so once the command is executed, The rules do not check for the need to rebuild targets that match other patterns. Look at an example:

#sample Makefile

Objects = foo.o bar.o

cflags: =-wall

%x:cflags +-G

%.o:cflags + +-o2

%.o%.x:%.c

$ (CC) $ (cflags) $<-o $@

When you perform "Make foo.o foo.x" on the command line, you see that only one file "FOO.O" is created, and make prompts the "foo.x" file to be up to date (in fact, "foo.x" is not created). This process indicates that the multiobjective pattern rules are handled as a whole when make is processed. This is the difference between the multiobjective pattern rules and the common rules of multiple goals. You may wish to change the above example to a common multi-objective rule to try what will be the result.

The last thing to say is:

1. The order of the pattern rules in makefile needs to note that when a target file conforms to multiple target patterns at the same time, make will take the pattern rule that matches the first target as the rule for rebuilding it.

2. The explicitly specified pattern rule in makefile overrides the implied pattern rule. That is, if there is a pattern rule in makefile that is appropriate to the target file, make does not look for other hidden rules for the target file, but uses the rule that appears in the makefile directly. When used, explicit rules always take precedence over implied rules.

3. In addition, the existence of dependent files, or the rules mentioned, prevail over those rules that require the use of implicit rules to create their dependent files.

Pattern Rule Example

This section looks at some examples of using pattern rules that have been predefined in GNU make. First look at the implied pattern rules for compiling. c files to. o Files:

%.O:%.c

$ (CC)-C $ (cflags) $ (cppflags) $<-o $@

This rule describes how an. o file is created by the corresponding. c file. The automation variable "$<" and "$@" are used in the command line of the rule, where the automation variable "$<" represents the dependency of the rule, and "$@" represents the target of the rule. When this rule is executed, the automation variable in the command line takes the corresponding value based on the actual target and dependent file. About automation variables refer to the 10.5.3 Automation variable section

The second inline pattern rule in make is:

%:: Rcs/%,v

$ (CO) $ (coflags) $<

The meaning of this rule is that any file "X" can be generated by the corresponding file "X.V" under the Directory "RCS". The target of the rule is "%", which matches any file name, so as long as the corresponding dependent file (N.V) exists, the target (N) can be created. A double colon indicates that the rule is a final rule, meaning that the dependent file of the rule is not an intermediate procedure file. Reference 106,000 with the rule section

In addition, an implied rule with multiple objectives is:

%.tab.c%.tab.h:%.y

bison-d $<

It is a multi-objective pattern rule, and the characteristics of multiple targets can be referenced in the 10.5.1 pattern rule Introduction to the last example of a section.

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.