Another way to do this :
Originally Posted by Mr.Lnx
The benefit is that you're not repeating things more than you need to - instead of repeating lists of files or actions several times, they're just in one place. That way when you add and remove files you only have to change them in one place rather than in several. Or if you modify the options you pass to gcc, you just have to do it in one place instead of for each .o:.c rule.
# Use variables for the target ($@ = demo here) and the dependencies ($^ = main.o f1.o f2.o)
demo : main.o f1.o f2.o
gcc -o $@ $^
# Define a generic rule for getting *.o from *.c. $< is the .c file in this case
%.o : %.c
gcc -c $<
# Add additional dependencies for header files. They are on top of the ".o needs a .c file"
# rule above, so no need to include the .c files in this list as well
main.o : f1.h
f1.o: f1.h f2.h
f2.o: f1.h f2.h