实用脚本 4 -- Makefile(不同文件下的多个可执行文件or静态库编译到同一目录下)
不同文件下的多个可执行文件编译到同一目录下,这样方便观察编译结果,从而方便进程操作。使用时根据自己的需要在进行局部修改(如 链接库、目标文件等等)。
1、.bashrc 中设置编译主目录(例如)
export MKHOME=$HOME
ORACLE_HOME=/oracle/10G; export ORACLE_HOME
2、主目录中创建一个mak文件夹,文件夹中创建以下三个文件(database.mak、mkstand.mak、platform.mak):
(1)database.mak(嵌入数据库文件编译配置选项,其中libclntsh.so为数据库中的库文件)
#|----------------------------------------------------------------------------| #| TOPMake 2.0 | #| Copyright (c) 2000-2003 XXX Software Systems Co., Ltd. | #| All Rights Reserved | #|----------------------------------------------------------------------------| #| FILE NAME : database.ora8.mak | #|----------------------------------------------------------------------------| DBINCP = . DBLIBP = -L$(ORACLE_HOME)/lib32 DBLIBS = -lclntsh DBD = DBDEF = -g DBFIX = .pc DBTYPE = #DBCHK = sqlcheck=full userid=$(ORACLEUSERID)/$(ORACLEUSERPWD)@$(ORACLETNSNAME) ~
(2)platform.mak(编译默认选项)
#|----------------------------------------------------------------------------| #| TOPMake 2.0 | #| Copyright (c) 2000-2003 XXX Software Systems Co., Ltd. | #| All Rights Reserved | #|----------------------------------------------------------------------------| #| FILE NAME : platform.mak | #|----------------------------------------------------------------------------| SHELL = /bin/sh CC = cc CXX = g++ DLINK = ld SLINK = ar DLFLG = -G SLFLG = rv CCFLG = -m32 -Wchar-subscripts -Wcomment -Wformat -Wmain -Wparentheses -Wsequence-point -Wreturn-type -Wfloat-equal -Wpointer-arith -Wbad-function-cast -Wcast-qual -Waggregate-return -g CXXFLG= EXFLG = -m32 -Wchar-subscripts -Wcomment -Wformat -Wmain -Wparentheses -Wsequence-point -Wreturn-type -Wfloat-equal -Wpointer-arith -Wbad-function-cast -Wcast-qual -Waggregate-return -g DBFLG = -g -O0 -DPRINT_DBOPR_LOG OPFLG = -g DLFIX = .so SLFIX = .a UXLIBS = -lm OSDEF = OSTYPE =
(3)mkstand.mak(编译基本规则)
#|----------------------------------------------------------------------------| #| TOPMake 2.0 | #| Copyright (c) 2000-2003 XXX Software Systems Co., Ltd. | #| All Rights Reserved | #|----------------------------------------------------------------------------| #| FILE NAME : mksstand.mak | #| DESCRIPTIONS : Makefile for all kind of platform, for c and pc file, | #| for lib(dynamic & static) and execuatable object | #|----------------------------------------------------------------------------| include $(MKHOME)/mak/platform.mak include $(MKHOME)/mak/database.mak # environment variables must be define outside of makefile # # $MKHOME : home directory of application # $CCFLG : compiler options for c source code # $CXXFLG : compiler options for c++ source code # $DLFLG : link options for dynamic library NO NEED FOR DYNAMIC LIBS && OMIT # $SLFLG : link options for static library # $EXFLG : link options for excecuteable object # $DBFLG : debug mode options # $OPFLG : optimize mode options BASICALLY NO HEAVY LOAD && MAY BE OMITTED # $DLFIX : suffix of dynamic library NO NEED FOR DYNAMIC LIBS && OMIT # $SLFIX : suffix of static library # $OBJS : c object files list # $LIBS : libs required for executable object # $DEFS : define flag list # $TARGET : target objetc name # commands PC = proc RM = rm -f CP = cp MV = mv CD = cd ECHO = @echo # paths and flags SYSINCP = /usr/include SYSLIBP = SRCP = $(MKHOME)/src BINP = $(MKHOME)/bin LIBP = $(MKHOME)/lib INCP = $(MKHOME)/incl INCPS = -I$(INCP) -I$(DBINCP) LIBPS = -L$(LIBP) $(DBLIBP) $(DBLIBS) CCFLGS = $(RLFLG) $(CCFLG) $(DEFS) $(DBDEF) $(OSDEF) $(BITDEF) $(INCPS) $(DBFLG) CXXFLGS = $(RLFLG) $(CXXFLG) $(DEFS) $(DBDEF) $(OSDEF) $(BITDEF) $(INCPS) $(DBFLG) DLFLGS = $(DLFLG) $(LIBPS) EXFLGS = $(EXFLG) $(LIBPS) SLTARG = $(LIBP)/lib$(TARGET)$(SLFIX) EXTARG = $(BINP)/$(TARGET) debugstatic: setdebug $(SLTARG) releasestatic: setrelease $(SLTARG) debugexec: setdebug $(EXTARG) releaseexec : setrelease $(EXTARG) setdebug: $(ECHO) $(DBFLG) > $(MKHOME)/mak/.mak.tmp setrelease: $(ECHO) $(OPFLG) > $(MKHOME)/mak/.mak.tmp $(SLTARG): $(OBJS) $(ECHO) "Linking [$(SLTARG)] ..." @$(SLINK) $(SLFLG) $@ $(OBJS) @$(RM) $(MKHOME)/mak/.mak.tmp $(EXTARG): $(OBJS) $(ECHO) "Linking [$(EXTARG)] ..." @$(CC) -o $@ $(EXFLGS) $(OBJS) $$LIBS @$(RM) $(MKHOME)/mak/.mak.tmp # implicit .SUFFIXES: .SUFFIXES: .pc .c .o .cpp .pc.o: $(ECHO) "Compiling [$@] ..." $(PC) include=$(ORACLE_HOME)/precomp/public include=$(INCP) lines=yes iname=$*$(DBFIX) $(DBCHK) $(CC) -o $@ `cat $(MKHOME)/mak/.mak.tmp` $(CCFLGS) -c $*.c $(RM) $*.lis $*.c tp* .cpp.o: $(ECHO) "Compiling [$@] ..." @$(CD) `dirname $@` ; \ $(CXX) -o $@ `cat $(MKHOME)/mak/.mak.tmp` $(CXXFLGS) -c `basename $<` .c.o: $(ECHO) "Compiling [$@] ..." @$(CD) `dirname $@`; \ $(CC) -o $@ `cat $(MKHOME)/mak/.mak.tmp` $(CCFLGS) -c `basename $<` # clearing object codes cleanstatic : cleanup @$(RM) $(SLTARG) cleanexec: cleanup @$(RM) $(EXTARG) cleanup: @$(RM) $(OBJS)
3、每个需要编译生成可执行的文件夹下创建makefile文件(内容如下)
#|----------------------------------------------------------------------------| #| TOPMake 2.0 | #| Copyright (c) 2000-2003 XXX Software Systems Co., Ltd. | #| All Rights Reserved | #|----------------------------------------------------------------------------| #| FILE NAME : sample.exec.mak | #| DESCRIPTIONS : Sample makefile for executable binary | #|----------------------------------------------------------------------------| ############################################################################### PRGOBJS1 = \ $(MKHOME)/src/XXX/XXX.o \ $(MKHOME)/src/XXX/XXX.o \ $(MKHOME)/src/XXX/XXX.o \ PRGTARG1 = XXX PRGLIBS1 = -lTransCust -lTools -lCommon -lcm -lfnm -lDbs -lHsm PRGDEFS1 = ############################################################################### debug all: debugstatic debugexec release: releasestatic releaseexec clean: cleanstatic cleanexec ##STATIC LIB releasestatic debugstatic cleanstatic: ##EXECUTABLE BIN releaseexec debugexec cleanexec: @make -f $(MKHOME)/mak/mkstand.mak $@ TARGET="$(PRGTARG1)" OBJS="$(PRGOBJS1)" LIBS="$(PRGLIBS1)" DEFS="$(PRGDEFS1)"
4、执行make可执行文件会生成在(BINP = $(MKHOME)/bin)所指定目录下 O(∩_∩)O~
5、静态库生成需要编译的文件夹下创建makefile文件和可执行文件略有不同(make一行写在 ##STATIC LIB下,有时不需要链接其他库 PRGLIBS1)如下:
#|----------------------------------------------------------------------------| #| TOPMake 2.0 | #| Copyright (c) 2000-2003 XXX Software Systems Co., Ltd. | #| All Rights Reserved | #|----------------------------------------------------------------------------| #| FILE NAME : sample.staticlib.mak | #| DESCRIPTIONS : Sample makefile for static library | #|----------------------------------------------------------------------------| ############################################################################### PRGOBJS1 = \ $(MKHOME)/src/Tools/xxx.o \ $(MKHOME)/src/Tools/xxx.o \ $(MKHOME)/src/Tools/xxx.o PRGTARG1 = Tools PRGDEFS1 =
############################################################################### debug all: debugstatic debugexec release: releasestatic releaseexec clean: cleanstatic cleanexec ##STATIC LIB releasestatic debugstatic cleanstatic: @make -f $(MKHOME)/mak/mkstand.mak $@ TARGET="$(PRGTARG1)" OBJS="$(PRGOBJS1)" LIBS="$(PRGLIBS1)" DEFS="$(PRGDEFS1)" ##EXECUTABLE BIN releaseexec debugexec cleanexec:
6、执行make静态库会生成在(BINP = $(MKHOME)/lib)所指定目录下 O(∩_∩)O~