共两个Makefile, Makefile1和Makefile2, 第1个生成静态库.a 第2个生成动态库.so
1. Makefile1 生成.a的Makefile
## 可以显示编译过程 最终生成libclientproto.a
### Makefile1 BEGIN ###
CC=g++
PROTO_FILES=$(wildcard *.proto)
SRC_FILES=$(PROTO_FILES:%.proto=../%.pb.cc)
INC_FILES=$(PROTO_FILES:%.proto=../%.pb.h)
OBJ_FILES=$(PROTO_FILES:%.proto=../%.pb.o)
DEP_FILES=$(PROTO_FILES:%.proto=../%.pb.d)
PROTOBUF_CFLAGS=$(shell pkg-config --cflags protobuf)
PROTOBUF_LFLAGS=$(shell pkg-config --libs protobuf)
CFLAGS=-I./ -Wall -Werror -fPIC -ggdb $(PROTOBUF_CFLAGS)
LFLAGS=$(PROTOBUF_LFLAGS)
ERRNO_INC_FILE=../cli_errno.h
ERRNO_XML=errno.xml
CMD_INC_FILE=../cli_cmd.h
CMD_XML=command.xml
CMD_MSG_CPP_FILE=../cmd_msg.cpp
ATTR_INC_FILE=../attr_type.h
ATTR_XML=attribute.xml
TARGET=../libclientproto.a
$(TARGET): $(SRC_FILES) $(OBJ_FILES) $(ERRNO_INC_FILE) $(CMD_INC_FILE) $(ATTR_INC_FILE) $(CMD_MSG_CPP_FILE)
ar r $(TARGET) $(OBJ_FILES)
$(ERRNO_INC_FILE): $(ERRNO_XML)
php tools/gen_errno.php $< > $@
$(CMD_INC_FILE): $(CMD_XML)
php tools/gen_cmd.php $< > $@
$(CMD_MSG_CPP_FILE): $(CMD_XML)
php tools/gen_cmd_msg.php $< > $@
$(ATTR_INC_FILE): $(ATTR_XML)
php tools/gen_attr.php $< > $@
cp ./attribute.xml ../../../online/design-conf/
#由于obj生成在$(OBJDIR)中,在依赖关系中加入目录
$(DEP_FILES): ../%.pb.d : ../%.pb.cc
@echo "generate dependence $@"
@printf "../" > $@.tmp
@$(CC) -MM $< $(CFLAGS) >> $@.tmp 2> /dev/null
@mv $@.tmp $@
$(OBJ_FILES) : ../%.pb.o : ../%.pb.cc
$(CC) -o $@ -c $< $(CFLAGS)
$(SRC_FILES) : ../%.pb.cc : %.proto
protoc --cpp_out=../ --proto_path=./ $<
clean:
-rm $(SRC_FILES) $(INC_FILES) $(OBJ_FILES) $(CMD_INC_FILE) $(ERRNO_INC_FILE) $(TARGET) $(DEP_FILES) $(ATTR_INC_FILE)
sinclude $(DEP_FILES)
### Makefile1 END ###
2. Makefile2 生成.so
## 可以显示编译过程 生成.so
### Makefile2 BEGIN ###
CC = g++
WORKDIR = ../
SRCDIR = $(WORKDIR)/src
LIBDIR = $(WORKDIR)/lib
OBJDIR = $(WORKDIR)/obj
DEPDIR = $(WORKDIR)/obj
INCLUDEDIR = $(WORKDIR)/src
BINDIR = $(WORKDIR)/bin
TARGET = $(BINDIR)/libonline.so
DATA_TARGET = $(BINDIR)/libdata.so
PROTODIR = $(WORKDIR)/../proto
PROTOBUF_CFLAGS = $(shell pkg-config --cflags protobuf)
PROTOBUF_LFLAGS = $(shell pkg-config --libs protobuf)
LIB_CLIENT_DIR = $(PROTODIR)/client
LIB_CLIENT_PROTO = $(LIB_CLIENT_DIR)/libclientproto.a
LIB_CLIENT_MAKEFILE_DIR = $(LIB_CLIENT_DIR)/define
LIB_HOME_DIR = $(PROTODIR)/home_svr
LIB_HOME_PROTO = $(LIB_HOME_DIR)/libhomeproto.a
LIB_HOME_MAKEFILE_DIR = $(LIB_HOME_DIR)/define
LIB_DB_DIR = $(PROTODIR)/db
LIB_DB_PROTO = $(LIB_DB_DIR)/libdbproto.a
LIB_DB_MAKEFILE_DIR = $(LIB_DB_DIR)/define
LIB_SWITCH_DIR = $(PROTODIR)/switch
LIB_SWITCH_PROTO = $(LIB_SWITCH_DIR)/libswitchproto.a
LIB_SWITCH_MAKEFILE_DIR = $(LIB_SWITCH_DIR)/
LIB_BATTLE_DIR = $(PROTODIR)/battle
LIB_BATTLE_PROTO = $(LIB_BATTLE_DIR)/libbtlproto.a
LIB_BATTLE_MAKEFILE_DIR = $(LIB_BATTLE_DIR)/define
LIB_FAMILY_DIR = $(PROTODIR)/family
LIB_FAMILY_PROTO = $(LIB_FAMILY_DIR)/libfamilyproto.a
LIB_FAMILY_MAKEFILE_DIR = $(LIB_FAMILY_DIR)/define
LIB_SYNC_DIR = $(PROTODIR)/sync
LIB_SYNC_PROTO = $(LIB_SYNC_DIR)/libsyncproto.a
LIB_SYNC_MAKEFILE_DIR = $(LIB_SYNC_DIR)/define
LIB_CACHE_DIR = $(PROTODIR)/cache
LIB_CACHE_PROTO = $(LIB_CACHE_DIR)/libcacheproto.a
LIB_CACHE_MAKEFILE_DIR = $(LIB_CACHE_DIR)/define
LIB_FIGHT_TEAM_DIR = $(PROTODIR)/fight_team
LIB_FIGHT_TEAM_PROTO = $(LIB_FIGHT_TEAM_DIR)/libfight_teamproto.a
LIB_FIGHT_TEAM_MAKEFILE_DIR = $(LIB_FIGHT_TEAM_DIR)/define
STATLOGGER_DIR = $(WORKDIR)/../statlogger
CFLAGS = -Wall -Werror -fPIC -ggdb \
-Iinclude -I/usr/local/lib/glib-2.0/include/ -I/usr/local/include/glib-2.0 \
-I/usr/lib/glib-2.0/include/ -I/usr/include/glib-2.0 \
-I$(WORKDIR)/../ -I/usr/include/libxml2 \
-DUSE_TLOG -DENABLE_TRACE_LOG -I./ -I./operation_201312 \
-I./operation_201402 -I./operation_201403 -I./operation_201404 -I./operation_201405 -I./operation_201406 \
-I./operation_201407 -I./operation_201408 -I./operation_201409 -I./operation_201410 -I./operation_201411 \
-I./operation_201412 -I./operation_201501
LFLAGS = -Wall -Werror -ggdb \
-L$(LIBDIR) -L$(LIB_CLIENT_DIR) -L$(LIB_SYNC_DIR) -L$(LIB_DB_DIR) -L $(LIB_SWITCH_DIR) -L$(LIB_BATTLE_DIR) \
-L$(LIB_FAMILY_DIR) -L$(LIB_HOME_DIR) -L$(LIB_CACHE_DIR) -L$(LIB_FIGHT_TEAM_DIR) -L$(STATLOGGER_DIR) \
-L./operation_201312 -L./operation_201402 -L./operation_201403\
-L./operation_201404 -L./operation_201405\
-L./operation_201406 -L./operation_201407 \
-L./operation_201408 -L./operation_201409 \
-L./operation_201410 -L./operation_201411 \
-L./operation_201412 -L./operation_201501 \
-lcrypto -pthread -lpthread -lm -lxml2 \
-lswitchproto -lsyncproto -lbtlproto -lfamilyproto -lhomeproto -lcacheproto -lfight_teamproto -lstatlogger \
-loperation_201312 -loperation_201402 -loperation_201403 -loperation_201404 -loperation_201405\
-loperation_201406 -loperation_201407 -loperation_201408 -loperation_201409 -loperation_201410 \
-loperation_201411 -loperation_201412 -loperation_201501 -lclientproto \
-ldbproto -ltaomee -ltaomee++
ALL_SRCS = $(wildcard $(SRCDIR)/*.cpp)
DATA_SRCS = $(shell ls $(SRCDIR)/global_data.cpp)
SRCS = $(shell ls -I "global_data*" $(SRCDIR)/*.cpp)
HEADERS = $(wildcard $(INCLUDEDIR)/*.h)
OBJS := $(patsubst %.cpp,%.o,$(subst $(SRCDIR),$(OBJDIR),$(SRCS)))
OBJS := $(subst $(OBJDIR)/global_data.o,,$(OBJS))
DATA_OBJS := $(patsubst %.cpp,%.o,$(subst $(SRCDIR),$(OBJDIR),$(DATA_SRCS)))
DEPS := $(patsubst %.cpp,%.d,$(subst $(SRCDIR),$(DEPDIR),$(SRCS)))
all: client_proto db_proto sync_proto switch_proto battle_proto family_proto home_proto cache_proto fight_team_proto \
operation201312 operation201402 operation201403 operation201404 operation201405 operation201406 operation201407 \
operation201408 operation201409 operation201410 operation201411 operation201412 operation201501 \
$(TARGET) $(DATA_TARGET)
cp ../../proto/client/define/attribute.xml ../design-conf/
cp ../../proto/client/attr_type.h attr_type.h.view
cp ./global_attr.h ../../helper/src/global_attr.h
#all: $(TARGET)
$(DATA_TARGET): $(DATA_OBJS)
g++ -shared -o $(DATA_TARGET) $(DATA_OBJS) $(LFLAGS) /usr/lib/libprotobuf.a
$(TARGET): $(LIB_CLIENT_PROTO) $(LIB_DB_PROTO) $(LIB_SWITCH_PROTO) \
$(LIB_BATTLE_PROTO) $(LIB_FAMILY_PROTO) $(LIB_HOME_PROTO) \
$(LIB_CACHE_PROTO) $(LIB_FIGHT_TEAM_PROTO) $(OBJS) ./operation_201312/liboperation_201312.a \
./operation_201402/liboperation_201402.a \
./operation_201403/liboperation_201403.a \
./operation_201404/liboperation_201404.a \
./operation_201405/liboperation_201405.a \
./operation_201406/liboperation_201406.a \
./operation_201407/liboperation_201407.a \
./operation_201408/liboperation_201408.a \
./operation_201409/liboperation_201409.a \
./operation_201410/liboperation_201410.a \
./operation_201411/liboperation_201411.a \
./operation_201412/liboperation_201412.a \
./operation_201501/liboperation_201501.a
g++ -shared -o $(TARGET) $(OBJS) $(LFLAGS) /usr/lib/libprotobuf.a
operation201312:
+make -C ./operation_201312
operation201402:
+make -C ./operation_201402
operation201403:
+make -C ./operation_201403
operation201404:
+make -C ./operation_201404
operation201405:
+make -C ./operation_201405
operation201406:
+make -C ./operation_201406
operation201407:
+make -C ./operation_201407
operation201408:
+make -C ./operation_201408
operation201409:
+make -C ./operation_201409
operation201410:
+make -C ./operation_201410
operation201411:
+make -C ./operation_201411
operation201412:
+make -C ./operation_201412
operation201501:
+make -C ./operation_201501
client_proto:
+make -C $(LIB_CLIENT_MAKEFILE_DIR)
db_proto:
+make -C $(LIB_DB_MAKEFILE_DIR)
sync_proto:
+make -C $(LIB_SYNC_MAKEFILE_DIR)
switch_proto:
+make -C $(LIB_SWITCH_MAKEFILE_DIR)
battle_proto:
+make -C $(LIB_BATTLE_MAKEFILE_DIR)
family_proto:
+make -C $(LIB_FAMILY_MAKEFILE_DIR)
home_proto:
+make -C $(LIB_HOME_MAKEFILE_DIR)
cache_proto:
+make -C $(LIB_CACHE_MAKEFILE_DIR)
fight_team_proto:
+make -C $(LIB_FIGHT_TEAM_MAKEFILE_DIR)
#由于obj生成在$(OBJDIR)中,在依赖关系中加入目录
$(DEPDIR)/%.d: $(SRCDIR)/%.cpp
@echo "generate dependence $@"
@printf "$(OBJDIR)/" > $@.tmp
@$(CC) -MM $< $(CFLAGS) >> $@.tmp 2> /dev/null
@mv $@.tmp $@
sinclude $(DEPS)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CC) -c $< -o $@ $(CFLAGS)
clean:
-rm $(OBJS) $(TARGET) $(DEPS) $(DATA_TARGET)
clean-target:
-rm $(TARGET)
clean-proto:
-rm $(LIB_CLIENT_PROTO) $(LIB_DB_PROTO) $(LIB_SWITCH_PROTO) $(LIB_BATTLE_PROTO) $(LIB_FAMILY_PROTO)
-rm $(LIB_HOME_PROTO) $(LIB_CACHE_PROTO)
-rm $(LIB_SYNC_PROTO) $(LIB_FIGHT_TEAM)
clean-all: clean
make -C $(LIB_CLIENT_MAKEFILE_DIR) clean
make -C $(LIB_DB_MAKEFILE_DIR) clean
make -C $(LIB_SWITCH_MAKEFILE_DIR) clean
make -C $(LIB_BATTLE_MAKEFILE_DIR) clean
make -C $(LIB_FAMILY_MAKEFILE_DIR) clean
make -C $(LIB_HOME_MAKEFILE_DIR) clean
make -C $(LIB_CACHE_MAKEFILE_DIR) clean
make -C $(LIB_SYNC_MAKEFILE_DIR) clean
make -C $(LIB_FIGHT_TEAM_MAKEFILE_DIR) clean
make -C operation_201312 clean
make -C operation_201402 clean
make -C operation_201403 clean
make -C operation_201404 clean
make -C operation_201405 clean
make -C operation_201406 clean
make -C operation_201407 clean
make -C operation_201408 clean
make -C operation_201409 clean
make -C operation_201410 clean
make -C operation_201411 clean
make -C operation_201412 clean
make -C operation_201501 clean
### Makefile2 END ###