首页 > 趣味生活 正文
在GNU Make中使用patsubst进行模式替换
在GNU Make中,patsubst是一个非常有用的函数,它允许我们可以使用模式替换的方式修改字符串。无论是修改文件名、路径名还是其他形式的字符串转换,patsubst都是一个功能强大的工具。
1. 理解patsubst函数的基本用法
patsubst函数的基本用法非常简单,它的语法如下:
$(patsubst PATTERN, REPLACEMENT, TEXT)
这个函数会在TEXT中查找出现的所有与模式PATTERN匹配的字符串,并将它们替换为REPLACEMENT。下面我们通过一个示例来理解这个函数的基本用法。
src_files := src/main.cpp src/utils/file.cpp src/models/model.cpp obj_files := $(patsubst src/%.cpp, obj/%.o, $(src_files))
在上述示例中,我们将src_files列表中的每个文件名中的src/前缀替换为obj/前缀,并将结果存储在obj_files变量中。如果src_files中的文件名是src/main.cpp,则经过patsubst处理后,obj_files中的对应项将成为obj/main.o。
2. 使用patsubst进行路径名的转换
patsubst函数在处理路径名时非常有用。我们可以通过patsubst轻松转换目录结构,例如在编译项目时将源文件分别放入不同的目录中,而不需要手动构建目标路径。
SRCS := src/main.cpp src/utils/file_utils.cpp src/models/model.cpp OBJS := $(patsubst src/%.cpp, obj/%.o, $(SRCS)) obj/%.o: src/%.cpp @mkdir -p $(@D) $(CXX) -c $< -o $@
在上述示例中,通过使用patsubst函数,我们将SRCS列表中的每个源文件路径从src/目录转换为obj/目录。在编译阶段,我们通过自动创建目标路径的方式,使用make命令自动将所有源文件编译为对应的目标文件。
3. patsubst函数的高级应用
patsubst还可以处理更复杂的字符串替换问题。例如,我们可以使用patsubst函数来自动构建目标文件的依赖关系。
SRCS := src/main.cpp src/utils/file_utils.cpp src/models/model.cpp OBJS := $(patsubst src/%.cpp, obj/%.o, $(SRCS)) DEPS := $(patsubst src/%.cpp, obj/%.d, $(SRCS)) -include $(DEPS) obj/%.o: src/%.cpp @mkdir -p $(@D) $(CXX) -MMD -c $< -o $@ obj/%.d: src/%.cpp @mkdir -p $(@D) $(CXX) -MM $< -MT $(patsubst obj/%.d, obj/%.o, $@) -MF $@
在上述示例中,我们使用patsubst函数生成了OBJS和DEPS两个变量。其中OBJS变量表示编译产生的目标文件列表,DEPS变量表示每个源文件的依赖关系文件。通过-include命令,我们将自动包含所有DEPS文件,从而在编译时自动处理依赖关系。
总结起来,patsubst函数在GNU Make中是一个非常实用的工具,它允许我们使用模式替换的方式修改字符串,从而实现各种字符串处理工作。无论是路径名转换、文件名修改还是其他字符串替换需求,patsubst都可以帮助我们轻松完成。
猜你喜欢
- 2023-08-21 paulina(Exploring Paulina's Journey A Tale of Perseverance and Dreams)
- 2023-08-21 pattaya(Exploring the Beauty of Pattaya)
- 2023-08-21 patsubst(在GNU Make中使用patsubst进行模式替换)
- 2023-08-21 patents(Revolutionizing Industries The Role of Patents in Innovation)
- 2023-08-21 partial(Partial Lunar Eclipse 2021 A Celestial Phenomenon Under the Lunar Glow)
- 2023-08-21 parker官网(Discover the Excellence of Parker Products)
- 2023-08-21 papertest(An Evaluation of the Impact of Papertests on Student Learning)
- 2023-08-21 paintschainer(Paintschainer Transforming Your Artwork with AI)
- 2023-08-21 package(Understanding Package in Programming)
- 2023-08-21 p2psearcher35下载(P2PSearcher35下载指南)
- 2023-08-21 overhead(Understanding Overhead The Hidden Costs of Business)
- 2023-08-21 outskirt(Exploring the Outskirts A Journey into the Lesser-known)
- 2023-08-21paulina(Exploring Paulina's Journey A Tale of Perseverance and Dreams)
- 2023-08-21pattaya(Exploring the Beauty of Pattaya)
- 2023-08-21patsubst(在GNU Make中使用patsubst进行模式替换)
- 2023-08-21patents(Revolutionizing Industries The Role of Patents in Innovation)
- 2023-08-21partial(Partial Lunar Eclipse 2021 A Celestial Phenomenon Under the Lunar Glow)
- 2023-08-21parker官网(Discover the Excellence of Parker Products)
- 2023-08-21papertest(An Evaluation of the Impact of Papertests on Student Learning)
- 2023-08-21paintschainer(Paintschainer Transforming Your Artwork with AI)
- 2023-08-10杭州西湖区邮编(西湖区邮编查询指南)
- 2023-08-11journey(我的旅程——探寻未知的世界)
- 2023-08-15四年级数学教学计划(四年级数学教学计划)
- 2023-08-14关于秋天的词语(秋日韵味)
- 2023-08-10广东陶瓷十大品牌(广东陶瓷十大品牌——打造高品质陶瓷产品)
- 2023-08-14石家庄市正定县(石家庄市正定县:一座历史悠久的文化古城)
- 2023-08-15赞美老师的诗句古诗(教师之美-赞美老师的诗句古诗)
- 2023-08-17300088股票(300088股票:深耕领域稳中求进)
- 2023-08-21ostaskcreate(OSTaskCreate函数详解)
- 2023-08-21origin75(Origin75:一款强大的数据分析与绘图软件)
- 2023-08-21office兼容包(Office兼容包:将办公更多样化)
- 2023-08-21norton官网(保护你的设备,让你的网络畅通无阻)
- 2023-08-21neilyoung(Neilyoung A Legend in the Music Industry)
- 2023-08-21neatimage(Neat Image Enhancing Image Quality with Advanced Noise Reduction)
- 2023-08-21mtv分享精灵官网(The Enchanted World of MTV Dive into the Official Website)
- 2023-08-21mp3铃声下载(如何免费下载MP3铃声)
- 猜你喜欢
-
- paulina(Exploring Paulina's Journey A Tale of Perseverance and Dreams)
- pattaya(Exploring the Beauty of Pattaya)
- patsubst(在GNU Make中使用patsubst进行模式替换)
- patents(Revolutionizing Industries The Role of Patents in Innovation)
- partial(Partial Lunar Eclipse 2021 A Celestial Phenomenon Under the Lunar Glow)
- parker官网(Discover the Excellence of Parker Products)
- papertest(An Evaluation of the Impact of Papertests on Student Learning)
- paintschainer(Paintschainer Transforming Your Artwork with AI)
- package(Understanding Package in Programming)
- p2psearcher35下载(P2PSearcher35下载指南)
- overhead(Understanding Overhead The Hidden Costs of Business)
- outskirt(Exploring the Outskirts A Journey into the Lesser-known)
- outlook自动回复(如何设置Outlook自动回复功能)
- ostaskcreate(OSTaskCreate函数详解)
- origin75(Origin75:一款强大的数据分析与绘图软件)
- office兼容包(Office兼容包:将办公更多样化)
- office下载(Office软件下载指南)
- officemac(Office for Mac - Streamlining Your Productivity)
- office2015(Microsoft Office 2015 A New Era of Productivity)
- office2007下载(如何下载Office 2007并成功安装)
- objective-c教程(Objective-C Basics Understanding the Fundamentals)
- numerous(Is It True That Numerous is the Key to Success)
- ntldrismissing(NTLDR is Missing A Troubleshooting Guide)
- ntfs文件系统(NTFS文件系统:Windows下最常见的文件系统)
- npptoolsdll(NPPTOOLSDLL A Powerful Tool for Enhancing Presentation Content)
- nova7se(华为nova7se:探索全新世界的引领者)
- norton官网(保护你的设备,让你的网络畅通无阻)
- nexus怎么读(Understanding the Nexus A Comprehensive Guide)
- netxray(NetXray An Insight into Network Traffic)
- neilyoung(Neilyoung A Legend in the Music Industry)