3

I'm quite new to but I still cannot understand how to set up the subdirectories of my source files.

My directory tree is:

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

I'm using 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)

2 Answers2

2

GNU make's wildcard function does not recursively visit all subdirectories. You need a recursive variant of it, which can be implemented as described in this answer:

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

So, instead of $(wildcard *.cpp) you need to use that recursive wildcard function.

Community
  • 1
  • 1
igagis
  • 1,959
  • 1
  • 17
  • 27
1

Another simpler way of finding files recursively might be to just use find.

For example, if you have a layout like this.

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

You could write a Makefile like this.

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

Which prints this.

$ 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
425nesp
  • 6,936
  • 9
  • 50
  • 61