make:对一个变量的替换引用不止一次? [英] make: substitution references on a variable more than once?
问题描述
在我的makefile中,干净"目标是通过使用SRC
变量中的替换引用来删除.c.bak
文件:
In my makefile, the 'clean' target is removing .c.bak
files by using substitution references in the SRC
variable:
rm -f $(SRC:.c=.c.bak)
这将导致它删除SRC
变量中所有不以.c
结尾的内容.
This causes it to delete everything in the SRC
var that doesn't end in .c
.
是否有一种优雅的方法可以对一个变量进行多个替换?例如,我也想尝试将.cpp
替换为.cpp.bak
...
Is there an elegant way to do more than one substitution on a variable? I would, for example, like to also attempt to substitute .cpp
with .cpp.bak
...
推荐答案
如果可能为所有源文件显示备份,而不仅仅是 .c 和 .cpp 文件,您可以相应地更改替换:
If backups might appear for all the source files, not just the .c and .cpp files, you could change the substitution accordingly:
rm -f $(SRC:=.bak)
或者,如果您使用的是GNU Make,则可以使用其文本处理功能来准确指出$(SRC)
中要影响的文件:
Or if you are using GNU Make, you could use its text processing functions to spell out exactly which files in $(SRC)
you want to affect:
rm -f $(addsuffix .bak, $(filter %.c %.cpp,$(SRC)))
一种更经典的方法是查看如何构造$(SRC)
变量的值.如果它是文件名的明确列表(应恕我直言,应该如此),则可以从中间变量为不同类型的源文件构建它:
A more classical approach would be to look at how you construct the value for your $(SRC)
variable. If it's an explicit list of filenames (as IMHO it should be) then you can build it up from intermediate variables for the different kinds of source file:
SRC_C = foo.c bar.c
SRC_CXX = baz.cpp apple.cpp
SRC = Makefile banana.pl carrot.pl $(SRC_C) $(SRC_CXX)
现在,您可以正确且方便地编写原始替换文件(即无需GNU Make工具):
Now you can write your original substitution correctly and portably (i.e., without requiring GNU Make facilities):
rm -f $(SRC_C:.c=.c.bak) $(SRC_CXX:.cpp=.cpp.bak)
这篇关于make:对一个变量的替换引用不止一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!