[英]Necessity of filter in GNU makefile
I have the following directory structure.我有以下目录结构。
/home/user/
├── Makefile
│
├── easy/
│ └── a.cpp
├── medium/
│ └── b.cpp
└── build/
My goal is to compile all .cpp
files in easy
and medium
and create separate executables in build
.我的目标是以
easy
和medium
的方式编译所有.cpp
文件,并在build
中创建单独的可执行文件。 The Makefile below achieves what I want (omitting the parts that are not relevant to my question).下面的 Makefile 实现了我想要的(省略与我的问题无关的部分)。 The part I do not understand is the necessity of the call to
filter
in line 6.我不明白的部分是在第 6 行调用
filter
的必要性。
1 SOURCEDIRS = easy medium
2 TARGETDIR = build
3
4 SOURCES = $(foreach dir,$(SOURCEDIRS),$(wildcard $(dir)/*.cpp))
5 $(info $(SOURCES))
6 TARGETS = $(foreach dir,$(SOURCEDIRS),$(patsubst $(dir)/%.cpp,$(TARGETDIR)/%.out,$(filter $(dir)/%,$(SOURCES))))
7 $(info $(TARGETS))
Output: Output:
# content of SOURCES
easy/a.cpp medium/b.cpp
# content of TARGETS
build/a.out build/b.out
Specifically, based on the the manual for patsubst
, I would expect the call to filter
to be redundant.具体来说,根据
patsubst
的手册,我希望对filter
的调用是多余的。
$(patsubst pattern,replacement,text)
$(patsubst 模式,替换,文本)
Finds whitespace-separated words in text that match pattern and replaces them with replacement.
在文本中查找与模式匹配的空格分隔的单词并将它们替换为替换。
Eg, I would expect the following Makefile to achieve my goal,例如,我希望以下 Makefile 能够实现我的目标,
1 SOURCEDIRS = easy medium
2 TARGETDIR = build
3
4 SOURCES = $(foreach dir,$(SOURCEDIRS),$(wildcard $(dir)/*.cpp))
5 $(info $(SOURCES))
6 TARGETS = $(foreach dir,$(SOURCEDIRS),$(patsubst $(dir)/%.cpp,$(TARGETDIR)/%.out,$(SOURCES)))
7 $(info $(TARGETS))
though the contents of TARGETS
are虽然
TARGETS
的内容是
# content of SOURCES
easy/a.cpp medium/b.cpp
# content of TARGETS
build/a.out medium/b.cpp easy/a.cpp build/b.out
What am I misunderstanding?我有什么误解? Why do we need
filter
in this case?为什么在这种情况下我们需要
filter
?
patsubst
doesn't delete the words that don't match the pattern. patsubst
不会删除与模式不匹配的单词。
$(patsubst easy/%.cpp,build/%.out,easy/a.cpp medium/b.cpp)
is not build/a.out
, it's build/a.out medium/b.cpp
$(patsubst easy/%.cpp,build/%.out,easy/a.cpp medium/b.cpp)
不是build/a.out
,而是build/a.out medium/b.cpp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.