问题描述
在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功能,可让您在同一行上链接多个替换项:.
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中具有多个扩展名的列表中每个文件的扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!