首页 > 代码库 > makefile示例

makefile示例

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=http://www.mamicode.com/$(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 = http://www.mamicode.com/util/md5.cpp /
queue.cpp

LIB_OBJ = md5.o \
queue.o

LIB = libmd5.a
EXE_SRC = http://www.mamicode.com/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