c语言通用Makefile

时间:2020-12-25 03:56:07

对于C语言小程序,可以用以下这个通用的Makefile

TGT =main
SRC = $(wildcard *.c)
OBJ =$(patsubst %.c,%.o,$(SRC))
CC =gcc

$(TGT): $(OBJ)
    $(CC) -o $@ $^ 
%.o:%.c
    $(CC) -c $<

include $(patsubst %.c,%.d,$(SRC))
%.d:%.c
    set -e; rm -f $@;\
        $(CC) -MM $< >$@.$$$$;\
        sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ >$@;\
        rm -f $@.$$$$
clean:
    $(RM) *.d *.o $(TGT)