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

相关推荐
笑川 孙14 天前
为什么Makefile中的clean需要.PHONY
开发语言·c++·面试·makefile·make·技术
浅安的邂逅23 天前
Linux Makefile-概述、语句格式、编写规则、多文件编程、Makefile变量分类:自定义变量、预定义变量
linux·c语言·vim·makefile·gcc
xyd陈宇阳25 天前
Linux 入门五:Makefile—— 从手动编译到工程自动化的蜕变
linux·运维·服务器·makefile
不摆烂选手1 个月前
Ubuntu之Makefile入门
linux·ubuntu·makefile·正点原子imx6ull学习笔记
azaz_plus1 个月前
Linux makefile的一些语法
linux·makefile
witton1 个月前
MinGW下编译ffmpeg源码时生成compile_commands.json
ffmpeg·json·makefile·mingw·调试·compile_command·remake
一朵忽明忽暗的云2 个月前
【Day9】make/makeFile如何让项目构建自动化起飞
linux·makefile·项目自动化构建工具
NullPointerExpection2 个月前
ubuntu20.04已安装 11.6版本 cuda,现需要通过源码编译方式安装使用 cuda 加速的 ffmpeg 步骤
c++·ffmpeg·makefile·cuda
半夏云流3 个月前
CMake常用命令指南(CMakeList.txt)
linux·makefile·cmake
利刃大大3 个月前
【Linux入门】2w字详解yum、vim、gcc/g++、gdb、makefile以及进度条小程序
linux·c语言·vim·makefile·gdb·gcc