Here’s a GNU makefile that:
- Takes three directory‐variables:
$(PARIS), $(LONDON) and $(ROME).
- Finds all
.cpp and .mm in each.
- Puts object‐files into
build/paris/, build/london/, build/rome/ respectively.
- Defines six separate pattern‐rules (one per directory+suffix).
# ┌─────────────────────────────────────────────────────────────────────┐
# │ User-configurable variables │
# └─────────────────────────────────────────────────────────────────────┘
PARIS ?= src/paris
LONDON ?= src/london
ROME ?= src/rome
CXX := g++
CXXFLAGS := -Wall -O2
# ┌─────────────────────────────────────────────────────────────────────┐
# │ 1) Gather sources in each dir, by suffix │
# └─────────────────────────────────────────────────────────────────────┘
SRC_CPP_PARIS := $(wildcard $(PARIS)/*.cpp)
SRC_MM_PARIS := $(wildcard $(PARIS)/*.mm)
SRC_CPP_LONDON := $(wildcard $(LONDON)/*.cpp)
SRC_MM_LONDON := $(wildcard $(LONDON)/*.mm)
SRC_CPP_ROME := $(wildcard $(ROME)/*.cpp)
SRC_MM_ROME := $(wildcard $(ROME)/*.mm)
# ┌─────────────────────────────────────────────────────────────────────┐
# │ 2) Compute corresponding object names in build/<city>/ │
# └─────────────────────────────────────────────────────────────────────┘
OBJ_CPP_PARIS := $(patsubst $(PARIS)/%.cpp, build/paris/%.cpp.o, $(SRC_CPP_PARIS))
OBJ_MM_PARIS := $(patsubst $(PARIS)/%.mm, build/paris/%.mm.o, $(SRC_MM_PARIS))
OBJ_CPP_LONDON := $(patsubst $(LONDON)/%.cpp, build/london/%.cpp.o, $(SRC_CPP_LONDON))
OBJ_MM_LONDON := $(patsubst $(LONDON)/%.mm, build/london/%.mm.o, $(SRC_MM_LONDON))
OBJ_CPP_ROME := $(patsubst $(ROME)/%.cpp, build/rome/%.cpp.o, $(SRC_CPP_ROME))
OBJ_MM_ROME := $(patsubst $(ROME)/%.mm, build/rome/%.mm.o, $(SRC_MM_ROME))
OBJECTS := \
$(OBJ_CPP_PARIS) $(OBJ_MM_PARIS) \
$(OBJ_CPP_LONDON) $(OBJ_MM_LONDON) \
$(OBJ_CPP_ROME) $(OBJ_MM_ROME)
# ┌─────────────────────────────────────────────────────────────────────┐
# │ 3) build dirs & main targets │
# └─────────────────────────────────────────────────────────────────────┘
BUILDDIRS := build/paris build/london build/rome
.PHONY: all clean
all: $(BUILDDIRS) myprogram
# Link all objects into your final binary:
myprogram: $(OBJECTS)
$(CXX) $(CXXFLAGS) $^ -o $@
# Ensure build subdirs exist before compiling any .o
$(BUILDDIRS):
mkdir -p $@
# ┌─────────────────────────────────────────────────────────────────────┐
# │ 4) Six separate pattern‐rules │
# └─────────────────────────────────────────────────────────────────────┘
# PARIS/.cpp → build/paris/*.cpp.o
build/paris/%.cpp.o: $(PARIS)/%.cpp | build/paris
$(CXX) $(CXXFLAGS) -c $< -o $@
# PARIS/.mm → build/paris/*.mm.o
build/paris/%.mm.o: $(PARIS)/%.mm | build/paris
$(CXX) $(CXXFLAGS) -x objective-c++ -c $< -o $@
# LONDON/.cpp → build/london/*.cpp.o
build/london/%.cpp.o: $(LONDON)/%.cpp | build/london
$(CXX) $(CXXFLAGS) -c $< -o $@
# LONDON/.mm → build/london/*.mm.o
build/london/%.mm.o: $(LONDON)/%.mm | build/london
$(CXX) $(CXXFLAGS) -x objective-c++ -c $< -o $@
# ROME/.cpp → build/rome/*.cpp.o
build/rome/%.cpp.o: $(ROME)/%.cpp | build/rome
$(CXX) $(CXXFLAGS) -c $< -o $@
# ROME/.mm → build/rome/*.mm.o
build/rome/%.mm.o: $(ROME)/%.mm | build/rome
$(CXX) $(CXXFLAGS) -x objective-c++ -c $< -o $@
# ┌─────────────────────────────────────────────────────────────────────┐
# │ 5) Clean │
# └─────────────────────────────────────────────────────────────────────┘
clean:
rm -rf build myprogram