Windows NDK编译protobuf动态库

编译环境:


Win7 64位
ndk-r10e
protobuf2.6.1(高版本理论上也能编译通过)
参考网址:https://blog.csdn.net/kaitiren/article/details/9047943

步骤:

1:下载protobuf源码

可根据需求下载所需要的版本,项目需求个人下载的是2.6.1
https://github.com/protocolbuffers/protobuf.git

2:准备编译源码

新建目录 protobuf-2.6.1_make(目录名称个人随意)
拷贝protobuf2.6.1下整个src文件夹到"protobuf-2.6.1_make"

源文件里面用到了config.h,而这个文件是在VS工程目录“vsprojects”下的,不在src/google/目录里面,所以需要把此文件copy到"protobuf-2.6.1_make/src"目录里面与"google"目录同级
windwos和linux系统存在差异,需要修改config.h文件
参考网址:https://blog.csdn.net/kaitiren/article/details/9047943
参考网址:https://blog.csdn.net/yixiao3660/article/details/51209422
--------config.h--------
#if _MSC_VER < 1310 || _MSC_VER >= 1600
#define HASH_NAMESPACE std
#else
//#define HASH_NAMESPACE stdext
//#endif

#ifdef _WIN32
    /* the location of <hash_set> */
    #define HASH_SET_H <hash_set>
    /* the location of <hash_map> */
    #define HASH_MAP_H <hash_map>
    #define HASH_NAMESPACE stdext
#else
    /* the location of <hash_set> */
    #define HASH_SET_H <ext/hash_set>
    /* the location of <hash_map> */
    #define HASH_MAP_H <ext/hash_map>
    #define HASH_NAMESPACE __gnu_cxx
#endif
#endif
------------------------------

3:配置Application.mk

新建目录jni
在jni目录下新建文件Application.mk
参考网址:https://blog.csdn.net/niuben127/article/details/78738671
参考网址:https://blog.csdn.net/momo0853/article/details/58049060

--------Application.mk--------
APP_ABI        := armeabi-v7a x86  #(32_bit(armeabi armeabi-v7a x86 mips), 64_bit(arm64-v8a x86_64 mips64))
APP_PLATFORM   := android-9     # "2.3" (3~25)
APP_STL        := gnustl_static #(system stlport_static stlport_shared gnustl_static gnustl_shared gabi++_static gabi++_shared)
APP_OPTIM      := release #(release debug)
APP_PIE        := true
APP_CPPFLAGS   := -frtti -std=c++11 -fexceptions 
------------------------------

4:配置Android.mk

在"protobuf-2.6.1_make"目录下新建文件Application.mk

https://blog.csdn.net/niuben127/article/details/78738671
https://blog.csdn.net/kaitiren/article/details/9047943

Android.mk配置的c++文件有两种方案获取

1.用VS打开“vsprojects”中的工程,下面有好几个工程,而对我们有用的其实只有一个,就是libprotobuf-lite和libprotobuf工程,看看里面都有什么.c和.cc或.cpp文件,把所有文件写进Android.mk
2.查看Makefile.am文件,找到libprotobuf_lite_la_SOURCES和libprotobuf_la_SOURCES定义的所有cc文件,把所有文件写进Android.mk

--------配置Android.mk--------
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := protobuf

#LOCAL_CFLAGS := -std=c++11 -fexceptions -frtti

LOCAL_CFLAGS := -DHAVE_PTHREAD -Wall -Wwrite-strings \
  -Woverloaded-virtual -Wno-sign-compare -Wno-unused-function
LOCAL_EXPORT_CFLAGS := $(LOCAL_CFLAGS)

LOCAL_SRC_FILES := \
  src/google/protobuf/stubs/atomicops_internals_x86_gcc.cc         \
  src/google/protobuf/stubs/atomicops_internals_x86_msvc.cc        \
  src/google/protobuf/stubs/common.cc                              \
  src/google/protobuf/stubs/once.cc                                \
  src/google/protobuf/stubs/stringprintf.cc                        \
  src/google/protobuf/extension_set.cc                             \
  src/google/protobuf/generated_message_util.cc                    \
  src/google/protobuf/message_lite.cc                              \
  src/google/protobuf/repeated_field.cc                            \
  src/google/protobuf/wire_format_lite.cc                          \
  src/google/protobuf/io/coded_stream.cc                           \
  src/google/protobuf/io/zero_copy_stream.cc                       \
  src/google/protobuf/io/zero_copy_stream_impl_lite.cc             \
  src/google/protobuf/stubs/strutil.cc                             \
  src/google/protobuf/stubs/strutil.h                              \
  src/google/protobuf/stubs/substitute.cc                          \
  src/google/protobuf/stubs/substitute.h                           \
  src/google/protobuf/stubs/structurally_valid.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_heavy.cc                       \
  src/google/protobuf/generated_message_reflection.cc              \
  src/google/protobuf/message.cc                                   \
  src/google/protobuf/reflection_ops.cc                            \
  src/google/protobuf/service.cc                                   \
  src/google/protobuf/text_format.cc                               \
  src/google/protobuf/unknown_field_set.cc                         \
  src/google/protobuf/wire_format.cc                               \
  src/google/protobuf/io/gzip_stream.cc                            \
  src/google/protobuf/io/printer.cc                                \
  src/google/protobuf/io/strtod.cc                                 \
  src/google/protobuf/io/tokenizer.cc                              \
  src/google/protobuf/io/zero_copy_stream_impl.cc                  \
  src/google/protobuf/compiler/importer.cc                         \
  src/google/protobuf/compiler/parser.cc        

LOCAL_C_INCLUDES := $(LOCAL_PATH)/src
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)

#LOCAL_STATIC_LIBRARIES := protobuf_lite
LOCAL_LDLIBS           := -llog -lz -lm
#include $(BUILD_STATIC_LIBRARY) #动态库还是静态库看实际使用情况
include $(BUILD_SHARED_LIBRARY)
------------------------------

5.编译库

命令行进入"protobuf-2.6.1_make/jni"
执行命令:ndk-build NDK_PROJECT_PATH=../ APP_BUILD_SCRIPT=../Android.mk

重大问题:实际使用过程中使用.so库在序列化和序列化时会出现崩溃问题,用.a库未出现,具体原因可能跟google的Android系统编译优化有关,可参考:http://blog.51cto.com/fengyuzaitu/2287421 中提到的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值