Using Implicit Rules

Letting make Deduce the Commands

It has an implicit rule for updating a .o file from a correspondingly named .c file using a cc -c command.

For example, it will use the command cc -c main.c -o main.o to compile main.c into main.o.

We can therefore omit the commands from the rules for the object files.

When a .c file is used automatically in this way, it is also automatically added to the list of prerequisites.

We can therefore omit the .c files from the prerequisites, provided we omit the commands.

bash 复制代码
# makefile中就一行
main:main.o
# 此时执行make
[root@kafka100 cpp]# make
cc -c -o main.o main.c
cc main.o -o main
# Because you mention main.o but do not give a rule for it, make will automatically look for an implicit rule that
# tells how to update it. 

# makefile中就一行
main:
# 此时执行make
[root@kafka100 cpp]# make
cc main.c -o main
bash 复制代码
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.
You can define your own implicit rules by writing pattern rules.

Defining and Redefining Pattern Rules

bash 复制代码
You define an implicit rule by writing a pattern rule.
A pattern rule looks like an ordinary rule, except that its target contains the character '%'.

the '%' matches any nonempty substring, while other characters match only themselves.
A target pattern is composed of a '%' between a prefix and a suffix, either or both of which may be empty.
'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.

Automatic Variables

bash 复制代码
It's very important that you recognize the limited scope in which automatic variable values are available:
they only have values within the recipe.

$@:The file name of the target of the rule.
$<:The name of the first prerequisite.
$^:The names of all the prerequisites, with spaces between them.
$?:The names of all the prerequisites that are newer than the target, with spaces between them.
If the target does not exist, all prerequisites will be included.

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

https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html

相关推荐
一个平凡而乐于分享的小比特3 天前
Linux内核构建三剑客:Kconfig、.config与Makefile关系详解
linux·makefile·kconfig·.config
ベadvance courageouslyミ5 天前
项目一(线程邮箱)
c语言·线程·makefile·进程间通信·线程邮箱
熊猫钓鱼>_>8 天前
Makefile应用场景实践日志:构建高效C/C++项目工作流
c语言·c++·bug·mfc·makefile·编译·kmp
___波子 Pro Max.19 天前
Makefile设置DEBUG宏定义方法总结
makefile·make
乖乖是干饭王25 天前
Linux 内核 Kbuild 中的 ld 调用机制
linux·c·makefile
fareast_mzh1 个月前
redis: undefined reference to `log‘
redis·makefile
冉佳驹1 个月前
Linux ——— sudo权限管理和GCC编译工具链的核心操作
linux·makefile·make·gcc·sudo·.phony
攻城狮7号1 个月前
【AI时代速通QT】第十节:在 Windows 上配置vs和qmake环境手动编译 Qt 项目
windows·qt·makefile·visual studio·qmake·vcvarsall·nmake/jom
青云交1 个月前
ShellCheck命令行工具适配开源鸿蒙PC实战指南
华为·开源·makefile·harmonyos·shellcheck·预编译二进制·hnp 打包
不知所云,1 个月前
3. cmake 和 Ninja安装
驱动开发·makefile·make·构建工具·ninja