The easiest way to find dependencies is to let g++ to tell you what are they by using -MT, -MD & -MM flags.
For example, lets say you have a project with 2 files, test.cpp and main.cpp, and they both link into an executable, so the makefile
that can handle dependencies is something like this:
Objects=Debug\test.o Debug\main.o
IncludePath=-I.
TestExec: $(Objects)
g++ -o TestExec $(Objects)
Debug\test.o: test.cpp Debug\test.o.d
g++ -c test.cpp -g -o Debug\test.o $(IncludePath)
Debug\test.o.d:
@g++ -g $(IncludePath) -MTDebug\test.o -MFDebug\test.o.d -MM test.cpp
Debug\main.o: main.cpp Debug\main.o.d
g++ -c main.cpp -g -o Debug\main.o $(IncludePath)
Debug\main.o.d:
@g++ -g $(IncludePath) -MTDebug\main.o -MFDebug\main.o.d -MM main.cpp
clean:
-if exist Debug\test.o del Debug\test.o
-if exist Debug\test.o.d del Debug\test.o.d
-if exist Debug\main.o del Debug\main.o
-if exist Debug\main.o.d del Debug\main.o.d
-if exist $(OutputFile) del $(OutputFile)
-if exist $(OutputFile).exe del $(OutputFile).exe
-include Debug/*.d
Note that for every object, I am adding another dependencie: <object_name>.d, for example:
Debug\test.o: test.cpp Debug\test.o.d
Now, the rule for 'Debug\test.o.d', is:
Debug\test.o.d:
@g++ -g $(IncludePath) -MTDebug\test.o -MFDebug\test.o.d -MM test.cpp
which invokes g++ preprocessor to create it. Open the .d file (it is a text file), and you will see there a single makefile rule generated by g++ for Debug\test.o, which includes all the dependencies required for test.o (I used here MM to omit system dependencies, such as stdio.h etc)
By chagning any of the file that test.cpp is including, the makefile will recompile it.
HTH,
Eran