you can use
makedepend to build the dependency list for you.
also did you know make has default actions for .c and .cpp files?
no need to expressly define them.
have a look at this - it's a make file I concocted for compiling and linking several C
files into a single target. Simple and effective.
It searches and creates a new dependency file each time. So you never have to touch
it even if you create new source files. Simply change *.c to *.cpp, add the link libraries and it would
work. (I think, maybe
)
Code:
TARGET=dummy_name
# ---------------------------------
# Standard makefile template
# just specify the target name above
# and the make file should sort it
# all out (hopefully)
# ---------------------------------
INCLUDEFILE = include.mk
# compiler options
# ============
# CFLAGS = -g -Wall
# this should appear above the 'include' directive below
# calls for the target and then the cleanup
# =========================================
all: ${TARGET} cleanup
-include ${INCLUDEFILE}
# ${INCLUDEFILE} should not exist when it's included.
# As it does not exist it is treated as a target.
# Therefore it is created anew each time,
# by the appropriate rule, below
# Which finds .c and .h files and calls makedepend for them
# =========================================================
${INCLUDEFILE}:
@echo "### creating ${INCLUDEFILE}"
@echo "### creating ${INCLUDEFILE}" > ${INCLUDEFILE}
-@ echo *.c|xargs echo SRCFILES = >> ${INCLUDEFILE}
-@ echo *.h|xargs echo HFILES = >> ${INCLUDEFILE}
@ makedepend -f${INCLUDEFILE} *.c
# (SRCFILES defined in INCLUDEFILE)
# =================================
OBJFILES = ${SRCFILES:.c=.o}
# this is the working target
# ==========================
${TARGET}: ${OBJFILES}
${CC} -o $@ ${OBJFILES}
help: print_help cleanup
print_help:
echo OBJFILES = ${OBJFILES}
echo SRCFILES = ${SRCFILES}
cat ${INCLUDEFILE}
clean:cleanup
-@ rm -f *.o
# we need to delete the INCLUDEFILE otherwise
# new source and header files won't be picked up
# next time
# =========
cleanup:
@echo "### deleting ${INCLUDEFILE}"
-@ rm -f ${INCLUDEFILE}