当前位置:网站首页>Makefile简介
Makefile简介
2022-06-28 10:51:00 【MoyangCitta】
Makefile主要被用来管理工程并实现自动化编译
Makefile规则
1.文件命名
makefile或Makefile
2.格式
(1)一个Makefile文件中可以有一个或者多个规则
目标:依赖
命令(Shell命令)- 目标:最终要生成的文件(伪目标除外)
- 依赖:生成目标所需要的文件或目标
- 命令:通过执行命令对依赖进行操作并生成目标(命令前必须Tab缩进)
(2)Makefile中其他规则一般都是为第一条规则服务的
工作原理
main:sub.c add.c main.c
gcc sub.c add.c main.c -o app1.命令在执行之前,需要先检查规则中的依赖项是否存在
- 如果存在,执行命令
- 如果不存在,向下检查其他规则,如果有相关规则则执行
2.检测更新,在执行规则中的命令时,会比较目标和依赖文件的时间
- 如果依赖的时间比目标时间晚,则重新生成目标
main:sub.o add.o main.o
gcc sub.o add.o main.c -o app
sub.o:sub.c
gcc -c sub.c -o sub.o
add.o:add.c
gcc -c add.c -o add.o
main.o:main.c
gcc -c main.c -o main.o虽然上式比之前要复杂,但是如果sub.c改变则只需要执行第一条规则和编译sub.o的规则,而之前的程序则需要重新执行,消耗更多系统资源。
变量
1.自定义变量
变量名=变量值(var=a.c b.c)
2.预定义变量
AR:归档维护程序的名称,默认值为ar
CC:C编译器的名称,默认值为cc
CXX:C++编译器的名称,默认值为g++
[email protected]:目标的完整名称
$<:第一个依赖文件的名称
$^:所有的依赖文件
3.获取变量的值
$(变量名) ($var)
main:main.c a.c b.c
gcc -c main.c a.c b.cmain:main.c a.c b.c
$(CC) -c $^ -o [email protected]模式匹配
%.o:%.c%:通配符,匹配一个字符串
两个%匹配的是同一个字符串
%.o:%.c
gcc -c $< -o [email protected]函数
$(wildcard PATTERN...)- 功能:获取指定目录下指定类型的文件列表
- 参数:PATTERN指的是某个或多个目录下的对应的某种类型的文件,如果有多个目录,一般使用空格间隔
- 返回:得到的若干个文件的文件列表,文件名之间使用空格间隔
示例:
$(wildcard *.c ./sub/*.c)返回值格式:a.c b.c
$(patsubst <pattern>,<replacement>,<text>)- 功能:查找<text>中的单词(单词以“空格”、“Tab”或“回车”“换行”分隔)是否符合模式<pattern>,如果匹配的话,则以<replacement>替换。
- <pattern>可以包括通配符“%”,表示任意长度的字串。如果<replacement>
中也包含“%”,那么, <replacement>中的这个“%”将是<pattern>中的那个%
所代表的字串。 (可以用“\”来转义,以“\%”来表示真实含义的“%”字符) - 返回:函数返回被替换过后的字符串
示例:将.c转换成.o
$(patsubst %.c, %.o, x.c bar.c)返回值格式:x.o bar.o
.PHONY伪目标:不与外部文件进行对比,可以有效地执行目标
示例:
.PHONY:clean
clean:
rm $(objs) -f如果当前文件夹中有与目标同名的文件,因为目标不含有依赖项,所以目标检测不到更新,无法执行clean操作,而伪目标可以防止此类现象
边栏推荐
- Katalon framework tests a web page operation example code
- Who knows if it is safe to open an account with CSC securities
- JS基础2
- 第六天 脚本与动画系统
- Yann Lecun's new paper: the road to building automatic agents
- Ruoyi integrated building block report (NICE)
- Décompression par compression
- MySQL (III)
- 树莓派无需显示屏的VNC Viewer方式的远程连接
- 工控安全之勒索病毒篇
猜你喜欢

无线模块透明传输技术的物联网应用案例

Several methods of using ABAP to operate Excel

移动命令

sentinel

Wireless communication module fixed-point transmission - point to multipoint specific transmission application

MarkDown——基本使用语法

一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!

Training and recognition of handwritten digits through the lenet-5 network built by pytorch

Katalon framework tests web (XX) custom keywords and upload pop-up operations

【实战】1364- 实现一个完美的移动端瀑布流组件(附源码)
随机推荐
Katalon framework tests a web page operation example code
fastposter v2.8.4 发布 电商海报生成器
Datetime and logging module
MySQL(二)
Interface automation framework scaffolding - Implementation of parametric tools
Realization of a springboard machine
[function suggestion] select a space when multiple workspaces are started
Mongo数据库
Summary of spatial temporal time series prediction modeling methods
数据库系列:有什么办法对数据库的业务表进行无缝升级
利用soapUI获取freemarker的ftl文件模板
Please consult me. I run the MYSQL to MySQL full synchronization of flykcdc in my local ide. This is in my local ide
Ruoyi integrated building block report (NICE)
How does ETF position affect spot gold price?
【实操】Appium Settings app is not running after 5000ms
Bytecode proof in appliedzkp zkevm (9)
AQS理解
Set up your own website (11)
乌国家安全与国防委员会秘书:将对俄境内目标进行精确打击
爱可可AI前沿推介(6.28)