资源描述:
《linux下c的通用makefile与解析》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Linux下C++的通用Makefile与解析#####################################################Genericmakefile-万能Makefile#forcompilingandlinkingC++projectsonLinux#Author:GeorgeFootModified:JackieLee#######################################################Customising##Adjustthefollowingifnecessary;EXECUTABLEisthe
2、target#executable'sfilename,andLIBSisalistoflibrariestolinkin#(e.g.alleg,stdcx,iostr,etc).Youcanoverridetheseonmake's#commandlineofcourse,ifyouprefertodoitthatway.##EXECUTABLE:=main #可执行文件名LIBDIR:= #静态库目录LIBS:= #静态库文件名INCLUDES:=. #头文件目录SRCDIR:= #除了当前目录外,其他的源代码文件目录###No
3、walteranyimplicitrules'variablesifyoulike,e.g.:CC:=g++CFLAGS:=-g-Wall-O3CPPFLAGS:=$(CFLAGS)CPPFLAGS+=$(addprefix-I,$(INCLUDES))CPPFLAGS+=-MMD###Thenextbitcheckstoseewhetherrmisinyourdjgppbin##directory;ifnotitusesdelinstead,butthiscancause(harmless)##`Filenotfound'errormessages.Ifyouarenotus
4、ingDOSatall,##setthevariabletosomethingwhichwillunquestioninglyremove##files.#RM-F:=rm-f##Youshouldn'tneedtochangeanythingbelowthispoint.#SRCS:=$(wildcard*.cpp)$(wildcard$(addsuffix/*.cpp,$(SRCDIR)))OBJS:=$(patsubst%.cpp,%.o,$(SRCS))DEPS:=$(patsubst%.o,%.d,$(OBJS))MISSING_DEPS:=$(filter-out$
5、(wildcard$(DEPS)),$(DEPS))MISSING_DEPS_SOURCES:=$(wildcard$(patsubst%.d,%.cpp,$(MISSING_DEPS))).PHONY:alldepsobjscleanverycleanrebuildinfoall:$(EXECUTABLE)deps:$(DEPS)objs:$(OBJS)clean:@$(RM-F)*.o@$(RM-F)*.dveryclean:clean@$(RM-F)$(EXECUTABLE)rebuild:verycleanallifneq($(MISSING_DEPS),)$(MISS
6、ING_DEPS):@$(RM-F)$(patsubst%.d,%.o,$@)endif-include$(DEPS)$(EXECUTABLE):$(OBJS)$(CC)-o$(EXECUTABLE)$(OBJS)$(addprefix-L,$(LIBDIR))$(addprefix-l,$(LIBS))info:@echo$(SRCS)@echo$(OBJS)@echo$(DEPS)@echo$(MISSING_DEPS)@echo$(MISSING_DEPS_SOURCES)注:1)命令行前的空白符必须为一个制表符(Tab);如,@$(RM-F)*.o前不是空格,而是一个制
7、表符;内容解析1.Makefile基本语法target为要生成的目标文件;dependency为target的依赖文件;command为用于生成target的命令行;:...(tab)(tab)...2.赋值符号:=与= :=与=的区别在于,符号:=表示立即展开变量值。例如:A:=fooB:=$(A)A:=bar这时,B的值仍为foo,因为它已被展开,不会再随A的值改变而改变。3.符号#是Ma