如何更改GNU make中具有多个扩展名的列表中每个文件的扩展名?

编程入门 行业动态 更新时间:2024-10-26 02:25:49
本文介绍了如何更改GNU make中具有多个扩展名的列表中每个文件的扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在GNU makefile中,我想知道是否可以通过文件列表输入来制作具有新扩展名的文件列表输出.

In a GNU makefile, I am wondering if it is possible, with an file list input, to make a file list output with new extensions.

在输入中,我得到以下列表:

In input, I get this list:

FILES_IN=file1.doc file2.xls

我想从 FILES_IN 变量在我的makefile中构建此变量:

And I would like to build this variable in my makefile from FILES_IN variable:

FILES_OUT=file1.docx file2.xlsx

有可能吗?怎么样?

这非常困难,因为我必须解析文件列表,并检测每个扩展名(.doc,.xls)以替换为正确的扩展名.

It's quite difficult because I have to parse file list, and detect each extension (.doc, .xls) to replace it to correct extension.

推荐答案

将扩展名替换为以空格分隔的文件名列表是常见的要求,并且具有内置功能.如果要在列表中每个名称的末尾添加x:

Substituting extensions in a list of whitespace-separated file names is a common requirement, and there are built-in features for this. If you want to add an x at the end of every name in the list:

FILES_OUT = $(FILES_IN:=x)

一般形式为$(VARIABLE:OLD_SUFFIX=NEW_SUFFIX).这将采用VARIABLE的值,并在每个以该后缀结尾的单词的末尾将OLD_SUFFIX替换为NEW_SUFFIX(不匹配的单词将保持不变). GNU make调用此功能(存在于每个make实现中)替换引用.

The general form is $(VARIABLE:OLD_SUFFIX=NEW_SUFFIX). This takes the value of VARIABLE and replaces OLD_SUFFIX at the end of each word that ends with this suffix by NEW_SUFFIX (non-matching words are left unchanged). GNU make calls this feature (which exists in every make implementation) substitution references.

如果只想使用此功能将.doc更改为.docx,将.xls更改为.xlsx,则需要使用中间变量.

If you just want to change .doc into .docx and .xls into .xlsx using this feature, you need to use an intermediate variable.

FILES_OUT_1 = $(FILES_IN:.doc=.docx) FILES_OUT = $(FILES_OUT_1:.xls=.xlsx)

您还可以使用稍微更通用的语法$(VARIABLE:OLD_PREFIX%OLD_SUFFIX=NEW_PREFIX%NEW_SUFFIX).该功能不是GNU make所独有的,但它不像普通的后缀更改那样具有可移植性.

You can also use the slightly more general syntax $(VARIABLE:OLD_PREFIX%OLD_SUFFIX=NEW_PREFIX%NEW_SUFFIX). This feature is not unique to GNU make, but it is not as portable as the plain suffix-changing substitution.

还有一个GNU make功能,可让您在同一行上链接多个替换项: patsubst函数.

There is also a GNU make feature that lets you chain multiple substitutions on the same line: the patsubst function.

FILES_OUT = $(patsubst %.xls,%.xlsx,$(patsubst %.doc,%.docx,$(FILES_IN)))

更多推荐

如何更改GNU make中具有多个扩展名的列表中每个文件的扩展名?

本文发布于:2023-11-02 17:53:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553031.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:扩展名   多个   如何更改   文件   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!