Makefile范例:单文件夹多个.c和.h编译
SRCDIR:=. OBJDIR:=. CC:=gcc STRIP:=strip CFLAGS:=-w -O2 .PHONY: all .DEFAULT: all OBJDIR:=./obj SRCS:=$(wildcard $(SRCDIR)/*.c) OBJS:=$(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(SRCS))) PROGRAM:=main.elf all: $(PROGRAM) $(OBJS): $(OBJDIR)/%.o : %.c @if [ ! -d $(dir $@) ] ; then mkdir -v -p $(dir $@) ; fi @echo compile: $< $(CC) $(CFLAGS) -c $< -o $@ $(PROGRAM): $(OBJS) $(CC) $(OBJS) -o $(PROGRAM) -lpthread -static $(STRIP) $(PROGRAM) clean: rm -f $(OBJDIR)/* $(PROGRAM)