码迷,mamicode.com
首页 > 其他好文 > 详细

makefile示例

时间:2014-07-24 21:34:52      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:style   color   使用   os   strong   文件   io   re   

1. 生成.so动态库

示例一

SoVer = 10010110
CfgVer = 10010110

BinName = fnights.so
GameName = "\"fnights\""
GameID = 100

BIN = $(BinName).$(SoVer)

all: clean compilerelease

clean:
    rm -f $(BIN) *.o

compilerelease:
    g++ -fPIC -O -shared -DNDEBUG -Wall -g -D_GAME_ID=$(GameID) -D_GAME_NAME=$(GameName) -D_CFG_VERSION=$(CfgVer) -o $(BIN) ./*.cpp

 注-fPIC:编译为位置独立的代码,so带上该选项以达到代码段共享的目的;-shared:编译为动态连接库

     -D:定义预编译值;-O:表示启用优化;-Wall:打印出gcc提供的警告信息;-o:指定输出的目标文件名

示例二

CPP = g++
SRCPATH = $(CURDIR)

SRCINCLUDE = $(SRCPATH)/Include/ServerFrame/
LIBSRCINCLUDE = $(SRCPATH)/Include/BaseLib/
COMMONDEF = $(SRCPATH)/Include/CommonDef/
PUBLIC_INC = ${HOME}/project/public/include/

INCLUDE = -I $(PUBLIC_INC) -I $(SRCINCLUDE) -I $(LIBSRCINCLUDE) -I $(COMMONDEF)

ALLLIBSPATH = -L$(SRCPATH)/Include/LibStatic/ -L/usr/local/lib -L${HOME}/project/public/lib -L$(SRCPATH)/Include/Lib/
SLIBS= -lbase
DLIBS= -lGameServer

C_ARGS = -g -Wall -rdynamic -D_DEBUG_ -D_POSIX_MT_ -D_FILE_OFFSET_BITS=64 -fstack-protector-all $(INCLUDE) -fPIC -O2
VERSION_STR = +++ This is a DEBUG Lib!! +++
BINARY = libGame.so

SUBDIR = Msg
SUBSRC=$(shell find $(SUBDIR) -name ‘*.cpp‘)
SUBOBJ=$(SUBSRC:%.cpp=%.o)

SO_OBJS = $(patsubst %.cpp,%.o,$(wildcard *.cpp)) $(patsubst %.c,%.o,$(wildcard *.c))
SOOBJS = $(SO_OBJS) $(SUBOBJ)

%.o : %.cpp
    $(CPP) $(C_ARGS) -c $< -o $@
%.o : %.c
    $(CPP) $(C_ARGS) -c $< -o $@

all : clean $(BINARY)

$(BINARY) : $(SOOBJS)
    rm -f $@
    $(CPP) $(C_ARGS) -o $@ $(SOOBJS) $(ALLLIBSPATH) -Wl,-Bstatic $(SLIBS) -Wl,-Bdynamic $(DLIBS) -fPIC -shared
    @echo
    @echo $(VERSION_STR)
    @echo

clean:
    rm -f $(SOOBJS)

 注-I:指定文件查找路径;-L:链接外部静态库与动态库的查找路径;

       -l:指定动态库与静态库的名称(最后库的文件名为:libbase.so、libGameServer.so或libbase.a、libGameServer.a),同时存在同名的静态库与动态库时,会优先匹配动态库;

       -rdynamic:指示连接器把所有符号(而不仅仅只是程序已使用到的外部符号)都添加到动态符号表(即.dynsym表)里,以便dlopen或backtrace等函数使用;

       -fstack-protector:启用堆栈保护,不过只为局部变量中含有char数组的函数插入保护代码;

       -Wl:告诉编译器将后面的参数传递给链接器;

       -Wl,-Bstatic:告诉链接器对接下来的-l选项使用静态链接;

       -Wl,-Bdynamic:告诉链接器对接下来的-l选项使用动态链接;

+++++++++++++++++++++++++++++++++++++++

动态库链接时搜索路径顺序如下:
(1)-L参数指定的路径
(2)环境变量LD_LIBRARY_PATH指定的路径
(3)配置文件/etc/ld.so.conf中指定的路径
(4)/lib; /usr/lib;

+++++++++++++++++++++++++++++++++++++++

2. 生成.a静态库

LibVer = 10010100

CFLAGS = $(CFLAG)
CC = gcc
CPP = g++
CFLAGS += -g -D_SOLIB_VERSION=$(LibVer) -DGAME_SO -I./ -I../Common -I../Externals/SDK/INCLUDE/COMMON -I../Externals/SDK/INCLUDE/SERVER -I./include -I./SoLogicCtrl
ADDLIB = ../Externals/SDK/solib/libSDK.a
LIB = libGame.a
all:$(LIB)

%.o : %.cpp
    $(CPP) $(CFLAGS) -c $< -o $@

%.o : %.c
    $(CC) $(CFLAGS) -c $< -o $@

ADDOBJS = BillReporter.o \
CreateLogic.o \
SoGameLogic.o \
./include/SvrCommonDef.o \
./SoLogicCtrl/SoBaseLogicCtrl.o \
../common/GameCommonDef.o

$(LIB): $(ADDOBJS)
    ar -rf $@ $(ADDOBJS)

install:
    cp $(BIN) /home/game/dll/$(BIN)
    chmod g+w /home/game/dll/$(BIN)

clean:
    rm -f ./*.o
    rm -f ./SoLogicCtrl/*.o
    rm -f ../Common/*.o
    rm -f ./include/*.o
    rm -f $(LIB)

+++++++++++++++++++++++++++++++++++++++

静态库链接时搜索路径顺序:
(1)-L参数指定的路径
(2)环境变量LIBRARY_PATH指定的路径
(3)/lib; /usr/lib; /usr/loacl/lib;

+++++++++++++++++++++++++++++++++++++++

3. 生成可执行程序

CC=gcc
CXX=g++

MOD = -g -D_DEBUG
INC = -Iinclude -Iutil

LIB_SRC = util/md5.cpp \
queue.cpp

LIB_OBJ = md5.o \
queue.o

LIB = libmd5.a
EXE_SRC = impl.cpp
EXE_OBJ = impl.o
EXE = clt

all: $(LIB) $(EXE)


$(LIB): $(LIB_OBJ)
    @rm -f $(LIB)
    ar q $(LIB) $(LIB_OBJ)

$(LIB_OBJ): $(LIB_SRC)
    $(CC) -c $(LIB_SRC) $(MOD) $(INC)

$(EXE): $(EXE_OBJ)
    $(CXX) -o $(EXE) $(EXE_OBJ) $(LIB)

$(EXE_OBJ): $(EXE_SRC)
    $(CC) -c $(EXE_SRC) $(MOD) $(INC)

.PHONY: clean
clean:
    -rm -f $(LIB) $(LIB_OBJ) $(EXE) $(EXE_OBJ)

最后会输出名为clt的可执行程序;clt依赖libmd5.a的静态库,libmd5.a有修改时,会先编译libmd5.a

makefile示例,布布扣,bubuko.com

makefile示例

标签:style   color   使用   os   strong   文件   io   re   

原文地址:http://www.cnblogs.com/kekec/p/3862689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!