【问题标题】:How do I recursively find source files in subdirectories in Makefile?如何在 Makefile 的子目录中递归查找源文件?
【发布时间】:2017-09-12 07:45:59
【问题描述】:

我对@9​​87654321@ 很陌生,但我仍然不明白如何设置我的源文件的子目录。

我的目录树是:

i18n/
src/
    engine/
    graphics/ (currently only directory used)

我正在使用this premade Makefile

TARGET = caventure
LIBS = -lSDL2
CC = g++
CFLAGS = -Wall
TGTDIR = build

.PHONY: default all clean

default: $(TARGET)
all: default

OBJECTS = $(patsubst %.cpp, %.o, $(wildcard *.cpp))
HEADERS = $(wildcard *.h)

%.o: %.cpp $(HEADERS)
    $(CC) $(CFLAGS) -c $< -o $@

.PRECIOUS: $(TARGET) $(OBJECTS)

$(TARGET): $(OBJECTS)
    $(CC) $(OBJECTS) -Wall $(LIBS) -o $(TGTDIR)/$(TARGET)

clean:
    -rm -f *.o
    -rm -f $(TARGET)

【问题讨论】:

    标签: makefile recursion makefile


    【解决方案1】:

    GNU make 的wildcard 函数不会递归访问所有子目录。 您需要它的递归变体,可以按照此答案中的描述实现:

    https://stackoverflow.com/a/18258352/1221106

    因此,您需要使用递归通配符函数,而不是 $(wildcard *.cpp)

    【讨论】:

      【解决方案2】:

      另一种递归查找文件的更简单方法可能是使用find

      例如,如果您有这样的布局。

      $ tree .
      .
      ├── d1
      │   └── foo.txt
      ├── d2
      │   ├── d4
      │   │   └── foo.txt
      │   └── foo.txt
      ├── d3
      │   └── foo.txt
      └── Makefile
      

      你可以这样写一个 Makefile。

      index.txt: $(shell find . -name "*.txt")                                             
          echo $^                                                                           
      

      打印这个。

      $ make
      echo d2/d4/foo.txt d2/foo.txt d1/foo.txt d3/foo.txt
      d2/d4/foo.txt d2/foo.txt d1/foo.txt d3/foo.txt
      

      【讨论】:

        猜你喜欢
        • 2017-03-26
        • 1970-01-01
        • 1970-01-01
        • 2015-07-19
        • 1970-01-01
        • 1970-01-01
        • 2010-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多