10Using Implicit Rules

Implicit rules tell make how to use customary techniques so that you do not have to specify them in detail
when you want to use them.

The built-in implicit rules use several variables in their recipes so that, by changing the values of the
variables, you can change the way the implicit rule works.
For example,the variable CFLAGS controls the flags given to the C compiler by the implicit rule for C compilation.

You can define your own implicit rules by writing pattern rules.

Using Implicit Rules

To allow make to find a customary method for updating a target file, all you have to do is refrain from specifying
recipes yourself.
Either write a rule with no recipe, or don’t write a rule at all.
Then make will figure out which implicit rule to use based on which kind of source file exists or can be made.
foo:foo.o bar.o
	cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
#Because you mention foo.o but do not give a rule for it, make will automatically look for an implicit rule that
#tells how to update it.
#This happens whether or not the file foo.o currently exists.
If an implicit rule is found,it can supply both a recipe and one or more prerequisites(the source files).
You would want to write a rule for foo.o with no recipe if you need to specify additional prerequisites, such as
header files, that the implicit rule cannot supply.

If you do not want an implicit rule to be used for a target that has no recipe, you can give that target an empty
recipe by writing a semicolon.

Catalogue of Built-In Rules

To see the full list of default rules and variables available in your version of GNU make, run ‘make -p’ in a
directory with no makefile.
#可以查看编译时make使用的变量,规则,main是程序名
make -p main > info_file

Variables Used by Implicit Rules

The recipes in built-in implicit rules make liberal use of certain predefined variables.
You can alter the values of these variables in the makefile, with arguments to make, or in the environment to alter
how the implicit rules work without redefining the rules themselves.

For example, the recipe used to compile a C source file actually says ‘$(CC) -c $(CFLAGS) $(CPPFLAGS)’. 

Defining and Redefining Pattern Rules

A pattern rule looks like an ordinary rule, except that its target contains the character ‘%’.
The target is considered a pattern for matching file names;the ‘%’ can match any nonempty substring, while other
characters match only themselves.

Note that expansion using ‘%’ in pattern rules occurs after any variable or function expansions, which take place
when the makefile is read. 
‘s.%.c’ as a pattern matches any file name that starts with ‘s.’, ends in ‘.c’ and is at least five characters long.
(There must be at least one character to match the ‘%’.)
The substring that the ‘%’ matches is called the stem.

‘%’ in a prerequisite of a pattern rule stands for the same stem that was matched by the ‘%’ in the target.

A pattern rule need not have any prerequisites that contain ‘%’, or in fact any prerequisites at all.
Such a rule is effectively a general wildcard.

https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html
https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
https://www.gnu.org/software/make/manual/html_node/Pattern-Rules.html

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值