Makefile通用模板

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:00   2762   0

Makefile通用模板

.PHONY: all clean

MKDIR = mkdir

RM = rm

RMFLAGS = -fr

CC = gcc

DIR_OBJS = objs

DIR_EXES = exes

DIR_DEPS = deps

DIRS = $(DIR_OBJS)$(DIR_EXES) $(DIR_DEPS)

EXE = complicated

EXE := $(addprefix$(DIR_EXES)/, $(EXE))

SRCS = $(wildcard *.c)

OBJS = $(SRCS:.c=.o)

OBJS := $(addprefix$(DIR_OBJS)/, $(OBJS))

DEPS = $(SRCS:.c=.dep)

DEPS := $(addprefix$(DIR_DEPS)/, $(DEPS))

all: $(EXE)

ifeq ("$(wildcard$(DIR_DEPS))", "")

DEPS_DIR_DEPS :=$(DIR_DEPS)

endif

ifneq($(MAKECMDGOALS), clean)

-include $(DEPS)

endif

$(DIRS):

$(MKDIR) $@

$(EXE): $(DIR_EXES)$(OBJS)

$(CC) -o $@ $(filter %.o, $^)

$(DIR_OBJS)/%.o:$(DIR_OBJS) %.c

$(CC) -o $@ -c $(filter %.c, $^)

$(DIR_DEPS)/%.dep:$(DEPS_DIR_DEPS) %.c

@echo "Making $@ ..."

@set -e; \

$(RM) $(RMFLAGS) $@.tmp; \

$(CC) -E -MM $(filter %.c, $^) >$@.tmp; \

sed 's,.\.o[ :]*,objs/\1.o $@: ,g' < $@.tmp >$@; \

$(RM) $(RMFLAGS) $@.tmp

clean:

$(RM) $(RMFLAGS) $(DIRS)


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP