通用多目录makefile的写法

时间:2021-03-17 04:42:34

我的项目文件层次是:项目名称/include、output、src

src/admin、stu、tch、common

最外层的Makefile:

DEBUG = y
MYDEBUG = DEBUG
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -D$(MYDEBUG)
else
DEBFLAGS =
endif

PRJ_HOME := $(shell pwd)
SOURCES := $(wildcard *.c)
OBJECTS := ${SOURCES:%.c=%.o}
INCPATH := -I$(PRJ_HOME)/include -I./

export CFLAGS += $(INCPATH)  $(DEBFLAGS)
export CC = gcc
export OUTPUTDIR = $(PRJ_HOME)/output

PROGRAM = system_stu
VPATH = ${OUTPUTDIR}

.PHONY : all
all : ${PROGRAM}

${PROGRAM} : ${OBJECTS} oadmin ocommon ostu otch
 $(CC) -o $@ ${OUTPUTDIR}/*.o $(CFLAGS)
%.o:%.c
 $(CC) -o ${OUTPUTDIR}/$@ -c $< $(CFLAGS)

oadmin :
 @$(MAKE) -C src/admin/
ocommon :
 @$(MAKE) -C src/common/
ostu :
 @$(MAKE) -C src/stu/
otch :
 @$(MAKE) -C src/tch/

.PHONY : clean
clean :
 $(RM) ${OUTPUTDIR}/*.o *.o ${PROGRAM} 

 

 

子目录的Makefile:

SOURCES := $(wildcard *.c)
OBJECTS := ${SOURCES:%.c=%.o}
VPATH = $(OUTPUTDIR)

all : ${OBJECTS}
%.o:%.c
 $(CC) -o ${OUTPUTDIR}/$@ -c $< $(CFLAGS)
clean :