Following the idea in “Recursive Make Considered Harmful” and other related posts, I finally build up a complete makefile for SOLID project.

The code is shown below.

MODULES := 

GCC := g++

CFLAGS += 

LIBS := 

# first include the source files in the root directory
SRC := $(wildcard *.cc)

# use module.mk to recursively extend SRC
include $(patsubst %, %/module.mk, $(MODULES))

# generate OBJ by replacing .cc to .o
OBJ := $(patsubst %.cc, %.o, $(SRC))


all: $(OBJ)
	$ (GCC) -of solid $ (OBJ) $ (LIBS)

%.o: %.cc
	$(GCC) -o $@ -c $< $(CFLAGS) $(LIBS)

# IMPORTANT: use .d file to indicate the dependency of .o and .d itself
-include $(OBJ:.o=.d)

# use -MM option in gcc to automatically generate the dependancy of .o and .d
%.d: %.cc
	./depend.sh `dirname $*.cc` $(CFLAGS) $*.cc > $@

# .PHONY: clean
clean:
	-rm solid $(OBJ) $(OBJ:.o=.d)

The code in depend.sh is shown below.

#!/bin/sh
DIR="$1"
shift 1
case "$DIR" in
"" | ".")
gcc -MM -MG "$@" |
sed -e `s@\(.*\)\.o:@\1.d \1.o:@`
;;
*)
gcc -MM -MG "$@" |
sed -e "s@\(.*\)\.o:@$DIR/\1.d $DIR/\1.o:@"
;;
esac

Just a memo.