参考了一个多目录下的Makefile的编写,但是有几处错误没法测试,这里作了改正,写了一模板,用了三个文件file1.h,file2.h,file3.h,test.c作测试。
1. 目录结构maketest下的子目录
bin:存放生成的文件
include:存放头文件(这里放入file1.h,file2.h,file3.h)
libs:存放引用的库
src:存放源文件(这里放入file1.c,file2.c,file3.c,test.c)
其中的Makefile文件如下
TOPDIR :=$(shell if [ "$$PWD" != "" ];then echo $$PWD;else pwd;fi)
export TOPDIR
LIBPATH:= $(TOPDIR)/libs
export LIBPATH
SUBDIRS =src
CPLUS_INCLUDE_PATH = $CPLUS_INCLUDE_PATH:$(TOPDIR)/include
export CPLUS_INCLUDE_PATH
.PHONY:$(SUBDIRS)
all:depend$(SUBDIRS)
dependdep:
@for dir in $(SUBDIRS);do $(MAKE) -C $$dir.depend;done
$(SUBDIRS):
$(MAKE) -C $@ all
clean:
@for dir in $(SUBDIRS);do $(MAKE) -C $$dirclean;done
2. src目录下的Makefile如下
SUBDIRS =ebook
.PHONY:$(SUBDIRS) clean
all:.depend$(SUBDIRS)
$(SUBDIRS):
$(MAKE) -C $@ all
.dependdep:
@for dir in $(SUBDIRS);do $(MAKE) -C $$dir.depend;done
clean:
@for dir in $(SUBDIRS);do $(MAKE) -C $$dirclean;done
3. ebook目录下的Makefile文件如下
INCLUDEDIRS+= -I/usr/include -I$(TOPDIR)/include
CC = gcc
CFLAGS = -O4 $(INCLUDEDIRS)
ELF = ../../bin/ebook #相对于可执行文件
AOBJS = $(patsubst %.s, %.o, $(wildcard *.s))
COBJS = $(patsubst %.c, %.o, $(wildcard *.c))
CPPOBJS =$(patsubst %.cpp, %.o, $(wildcard *.cpp))
OBJS = $(AOBJS) $(COBJS) $(CPPOBJS)
all:.depend $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -L $(LIBPATH) \
-L /usr/lib -ljpeg -lfreetype -lttf -lpthread \
-L /usr/X11R6/lib \
-o $(ELF)
chmod 755 $(ELF)
.depend:Makefile $(AOBJS:.o=.S) $(COBJS:.o=.c) $(CPPOBJS:.o=.cpp)
$(CC) -M $(CFLAGS) $(AOBJS:.o=.S)$(COBJS:.o=.c) $(CPPOBJS:.o=.cpp) > $@
sinclude.depend
clean:
rm -f *.o .depend *.out *.bak
rm-f $(ELF)