Skip to content

Instantly share code, notes, and snippets.

@tcoffin
Created May 2, 2014 15:30
Show Gist options
  • Save tcoffin/8df26e9df8d3f93d962e to your computer and use it in GitHub Desktop.
Save tcoffin/8df26e9df8d3f93d962e to your computer and use it in GitHub Desktop.
Example makefile with proper dependency handling
SRCS := foo.c bar.c
OBJS := $($(SRC):.c=.o)
foo: $(OBJS)
$(CPP) -o $@ $^ $(LINKFLAGS)
-include $(OBJS:.o=.d)
%.o: %.cc
$(CPP) -c $(CFLAGS) $*.cc -o $*.o
$(CPP) -MM $(CFLAGS) $*.cc > $*.d # generates .d files which will detect header file changes as well
@sed -i -e 's|.*:|$*.o:|' $*.d # corrects a problem in the generated .d files
%.o: %.c
$(CC) -c $(CFLAGS) $*.c -o $*.o
$(CC) -MM $(CFLAGS) $*.c > $*.d
@sed -i -e 's|.*:|$*.o:|' $*.d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment