天天看點

通用多目錄makefile的寫法

我的項目檔案層次是:項目名稱/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:

VPATH = $(OUTPUTDIR)

all : ${OBJECTS}