标签:style class blog code http tar
经测试,完全满足了要求,虽然不是完美无缺,但是对于通常的工程项目,是绰绰有余了,欢迎大家指正,以便改善。
另外对于Makefile的理解,个人认为可以分成几个部分:依赖关系,自定义变量和自动变量以及Makefile提供的相关函数。理解了它们,对写出结构良好,通用性强的Makefile会有大的帮助,虽然现在有了automake,但是研究一下Makefile的写法还是有收获的。
附件testmk.rar中提供了一个完整的测试程序,目录如下:
在linux下解压开后运行命令一下命令,可以根据需要定制Makefile。
参考资料:
思考:
根据Makefile的依赖原理,似乎应该使用一个树类型的结构,a依赖b,c等,b又依赖其他的文件,但是不能反方向依赖(否则为死循环),但是d可以同时依赖b,c,所以不是简单的树形结构,应该是一个图,另外依赖是有方向性的,所以应该是一个有向图结构的实现,不知道对不对?
# A generic template Makefile # Author Red_Liu lli_njupt@163.com v0.1 # # This file is a(part of) free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2, or # (at your option) any later version. .PHONY = all install clean #global directory defined TOPDIR = $(shell pwd) SRCDIR = $(TOPDIR)/src LIBDIR = $(TOPDIR)/lib OBJECTDIR = $(TOPDIR)/build INCLUDEDIR = $(TOPDIR)/include #cross compile tools defined CROSS_COMPILE ?= AS = $(CROSS_COMPILE)as LD = $(CROSS_COMPILE)ld CC = $(CROSS_COMPILE)gcc CPP = $(CC) -E AR = $(CROSS_COMPILE)ar NM = $(CROSS_COMPILE)nm STRIP = $(CROSS_COMPILE)strip RANLIB = $(CROSS_COMPILE)ranlib #local host tools defined CP := cp RM := rm MKDIR := mkdir SED := sed FIND := find MKDIR := mkdir XARGS := xargs #target name TARGETMAIN = testmk TARGETLIBS = libmk.a TARGETSLIBS = libmk.so #FILE‘ INFOMATION COLLECT VPATH = $(shell ls -AxR $(SRCDIR)|grep ":"|grep -v "\.svn"|tr -d ‘:‘) SOURCEDIRS = $(VPATH) #search source file in the current dir SOURCES = $(foreach subdir,$(SOURCEDIRS),$(wildcard $(subdir)/*.c)) SRCOBJS = $(patsubst %.c,%.o,$(SOURCES)) BUILDOBJS = $(subst $(SRCDIR),$(OBJECTDIR),$(SRCOBJS)) DEPS = $(patsubst %.o,%.d,$(BUILDOBJS)) #external include file define CFLAGS = -O2 -Wall -MD $(foreach dir,$(INCLUDEDIR),-I$(dir)) ARFLAGS = rc #special parameters for apps CFLAGS += #c file compile parameters and linked libraries CPPFLAGS = LDFLAGS = XLDFLAGS = -Xlinker "-(" $(LDFLAGS) -Xlinker "-)" LDLIBS += -L $(LIBDIR) #defaut target:compile the currrent dir file and sub dir all: $(TARGETMAIN) #for .h header files dependence -include $(DEPS) $(TARGETMAIN) :$(BUILDOBJS) @$(CC) $(subst $(SRCDIR),$(OBJECTDIR),$^) $(CPPFLAGS) $(CFLAGS) $(XLDFLAGS) -o $@ $(LDLIBS) @$(STRIP) --strip-unneeded $(TARGETMAIN) $(TARGETLIBS) :$(BUILDOBJS) @$(AR) $(ARFLAGS) $@ $(BUILDOBJS) @$(RANLIB) $@ $(TARGETSLIBS) :$(BUILDOBJS) @$(CC) -shared $(subst $(SRCDIR),$(OBJECTDIR),$^) $(CPPFLAGS) $(CFLAGS) $(XLDFLAGS) -o $@ $(LDLIBS) $(OBJECTDIR)%.o: $(SRCDIR)%.c @[ ! -d $(dir $(subst $(SRCDIR),$(OBJECTDIR),$@)) ] & $(MKDIR) -p $(dir $(subst $(SRCDIR),$(OBJECTDIR),$@)) @$(CC) $(CPPFLAGS) $(CFLAGS) -o $(subst $(SRCDIR),$(OBJECTDIR),$@) -c $< intall: clean: @$(FIND) $(OBJECTDIR) -name "*.o" -o -name "*.d" | $(XARGS) $(RM) -f @$(RM) -f $(TARGETMAIN) $(TARGETLIBS) $(TARGETSLIBS)
一个通用的Makefile (转),布布扣,bubuko.com
标签:style class blog code http tar
原文地址:http://www.cnblogs.com/welhzh/p/3799002.html