简体   繁体   English

GNU makefile 中过滤器的必要性

[英]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 .我的目标是以easymedium的方式编译所有.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.在文本中查找与模式匹配的空格分隔的单词并将它们替换为替换。

source 资源

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM