因为不想排版 所以这里只有文字 可以下载word版观看:点击下载
作用:添加protobuffer支持后,可以利用protobuffer特性来序列化和反序列化二进制数据流。
软件准备:cocos2d-x3.2, protobuffer2.5.0,protoc-2.5.0-win32, vs2012, anroid相关环境(SDK, NDK , CYGWIN等)
安装完成cocos2d-x3.2 配置好环境变量,利用命令新增项目
命令 “cocos new ProtobufferDemoWithLib -p com.jcracker.ProtobufferDemoWithLib -l cpp -d/”
创建成功界面:
模仿external模块。将protobuffer文件复制到external中。
生成pb.h和pb.cc
命令“protoc TestName.proto --cpp_out.”
可以看到 其中头文件“#include <google/protobuf/stubs/common.h>
”均找不到文件。所以设置项目的头文件路径到protobuffer
显示正常
编译通过
编译运行
至此 win32平台protobuffer库添加完成。
用eclipse导入
找不到gen是因为没设置一个选项
先注释掉在win32平台上添加的 TestName,保证最原始的android能编译通过
碰到这个错误 记得复制一份源码就可以解决,解决方法如下:
将它复制一份到
Ok 搞定
Eclipse中编译运行 构建成功
原文如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
TARGET_CPU_ABI := armeabi
#TARGET_CPU_ABI := armeabi-v7a
#TARGET_CPU_ABI := armeabi-v7a-hard
#TARGET_CPU_ABI := armeabi
#设置c++11头文件路径和链接路径
prebuilt_stdcxx_PATH := E:\android-ndk-r9d-windows-x86_64\android-ndk-r9d\sources\cxx-stl\gnu-libstdc++\4.8
LOCAL_C_INCLUDES += \
$(prebuilt_stdcxx_PATH)/include \
$(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI)/include
LOCAL_LDFLAGS += -L$(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI) -lgnustl_static -lsupc++
LOCAL_MODULE := protobuf_static
LOCAL_MODULE_FILENAME := libprotobuf
LOCAL_CPP_EXTENSION := .cc
LOCAL_SRC_FILES := \
src/google/protobuf/io/coded_stream.cc\
src/google/protobuf/stubs/common.cc \
src/google/protobuf/descriptor.cc \
src/google/protobuf/descriptor.pb.cc\
src/google/protobuf/descriptor_database.cc\
src/google/protobuf/dynamic_message.cc\
src/google/protobuf/extension_set.cc\
src/google/protobuf/extension_set_heavy.cc\
src/google/protobuf/generated_message_reflection.cc \
src/google/protobuf/generated_message_util.cc \
src/google/protobuf/io/gzip_stream.cc \
src/google/protobuf/compiler/importer.cc\
src/google/protobuf/message.cc\
src/google/protobuf/message_lite.cc \
src/google/protobuf/stubs/once.cc \
src/google/protobuf/compiler/parser.cc\
src/google/protobuf/io/printer.cc \
src/google/protobuf/reflection_ops.cc \
src/google/protobuf/repeated_field.cc \
src/google/protobuf/service.cc\
src/google/protobuf/stubs/structurally_valid.cc \
src/google/protobuf/stubs/strutil.cc\
src/google/protobuf/stubs/substitute.cc \
src/google/protobuf/stubs/stringprintf.cc \
src/google/protobuf/text_format.cc\
src/google/protobuf/io/tokenizer.cc \
src/google/protobuf/unknown_field_set.cc\
src/google/protobuf/wire_format.cc\
src/google/protobuf/wire_format_lite.cc \
src/google/protobuf/io/zero_copy_stream.cc\
src/google/protobuf/io/zero_copy_stream_impl.cc \
src/google/protobuf/io/zero_copy_stream_impl_lite.cc
#LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include/protobuf
#LOCAL_C_INCLUDES := $(LOCAL_PATH)/include/protobuf
#LOCAL_CFLAGS := -std=c99
#LOCAL_CFLAGS += -std=c++11 -stdlib=libstdc++ -Wno-error=c++11-narrowing
include $(BUILD_STATIC_LIBRARY)
原文如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos)
LOCAL_MODULE := cocos2dcpp_shared
LOCAL_MODULE_FILENAME := libcocos2dcpp
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
# LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static
# LOCAL_WHOLE_STATIC_LIBRARIES += spine_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
LOCAL_WHOLE_STATIC_LIBRARIES += protobuf_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,.)
$(call import-module,audio/android)
# $(call import-module,Box2D)
# $(call import-module,editor-support/cocosbuilder)
# $(call import-module,editor-support/spine)
# $(call import-module,editor-support/cocostudio)
# $(call import-module,network)
# $(call import-module,extensions)
$(call import-module,external/protobuf-2.5.0)
必须保证全部正常 否则后面会出现依赖问题
这里如果出现错误 比如缺少包 工具 请运行cygwin setup.exe 选择internet模式 添加
如果出现找不到google/protobuf头文件之类的。 将protobuffer中src下的所有.h拷贝一份 放到ndk的平台相关目录中:
开始编译protobuffer
链接 出现这种错误
是因为我们头文件是用的gun stl 因此去修改 Application.mk 中的APP_STL为gunstl_static即可
原文如下:
APP_STL := gnustl_static
NDK_TOOLCHAIN_VERSION=clang
APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -std=c++11 -fsigned-char
APP_LDFLAGS := -latomic
APP_DEBUG := $(strip $(NDK_DEBUG))
ifeq ($(APP_DEBUG),1)
APP_CPPFLAGS += -DCOCOS2D_DEBUG=1
APP_OPTIM := debug
else
APP_CPPFLAGS += -DNDEBUG
APP_OPTIM := release
Endif
成功被编译
运行在小米3上 出现JCracker TestName
这里截图不方便 就不放图了
接下来 做序列化和反序列化
TODO IOS支持