一天下来,Makefile终于完成了,可以实现c文件和头文件不同目录,并将输出文件输出到其他如output目录,将执行文件和makefile文件并列:
源码如下:
# ============================================================
# Makefile for project (by chen long 2019.03.29) ======
# make clean : clean output/ ======
# make : make clean && make all ======
# ============================================================
TARGET := app
MKDIR := mkdir -p
RM := rm -rf
CC := gcc #arm-linux-gcc
# LIBS := -lpthread
CFLAGS := -o
MAKE_PATH := $(CURDIR)
INC_PATH := -I$(MAKE_PATH)/include
SRC_PATH := $(MAKE_PATH)/src
OUT_PATH := $(MAKE_PATH)/output
# ---------------------------------------------------------
# ---------------------------------------------------------
SRCS_DIR =$(wildcard $(SRC_PATH)/*.c)
SRCS = $(notdir $(SRCS_DIR) )
OBJS = $(patsubst %.c,%.o,$(SRCS))
OBJS_DIR = $(addprefix $(OUT_PATH)/, $(OBJS))
default: all
.PHONY: mk_output_dir clean cleanall
all:clean mk_output_dir $(OBJS) $(TARGET)
# 搜索目录下.c和.o文件
vpath %.c $(SRC_PATH)
vpath %.o $(OUT_PATH)
mk_output_dir:
@$(MKDIR) $(OUT_PATH)
$(OBJS):%.o:%.c
@$(CC) $(INC_PATH) $(CFLAGS) $(OUT_PATH)/$@ -c $<
$(TARGET) :$(OBJS_DIR)
@$(CC) $(INC_PATH) $(LIBS) $(CFLAGS) $@ $^
clean:
$(RM) $(OUT_PATH)
cleanall:
$(RM) $(OUT_PATH) $(MAKE_PATH)/main