Originally Posted by
Mr.Lnx
Code:
demo : main.o f1.o f2.o
gcc -o demo main.o f1.o f2.o
main.o : main.c f1.h
gcc -c main.c
f1.o: f1.c f1.h f2.h
gcc -c f1.c
f2.o: f2.c f1.h f2.h
gcc -c f2.c
Another way to do this :
Code:
# 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
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.