diff --git a/config/rules.mk b/config/rules.mk
index 815fefd1c1496112533e3de87e6d40978e5e05cc..df832a3948feab95ab2243a46282d899956b9175 100644
--- a/config/rules.mk
+++ b/config/rules.mk
@@ -23,8 +23,14 @@ depend:
 depend.mk:
 	@echo Building dependency file depend.mk
 	@for srcfile in `find . -name "*.c" -or -name "*.cc" -or -name "*.cxx" -or -name "*.cpp" -or -name "*.inc" -or -name "*.hpp"` verylongdummyname ; do \
-		if [ -f $$srcfile ] ; then \
-			${CC} -MM ${INCFLAGS} $$srcfile >> depend.mk ; \
+		if [ -f "$${srcfile}" ] ; then \
+			basename="$${srcfile%.*}" ; \
+			basename="$${basename#./}" ; \
+			objfile="$${basename}.o" ; \
+			depfile="$${basename}.mk" ; \
+			${CC} -MM -MT "${BUILDDIR}$${objfile}" -MF "$${depfile}" ${CPPFLAGS} ${INCFLAGS} "$${srcfile}" > /dev/null; \
+			cat "$${depfile}" >> depend.mk ; \
+			rm -f "$${depfile}" ; \
 		else \
 			touch depend.mk; \
 		fi \