用Cygwin把Protobuf编译到安卓环境下

把Protobuf编译到安卓环境下有点难,下面是具体步骤

1、把我们解压后的文件复制到下图路径中


一定要注意文件路径啊,我们下载解压后为双层文件夹,我把那个没用的去掉了,我的protobuf-2.5.0文件夹下直接就是各种文件了,并不是protobuf-2.5.0文件夹下又个

protobuf-2.5.0文件夹。

2、为protobuffer添加编译所需的头文件支持。 

打开Cygwin,把protobuf-2.5.0文件夹中的configure文件拖到Cygwin中点击运行,


运行完毕后在输入  make 点击运行


运行完毕后输入 make install 点击运行


3、拷贝文件到NDK

上面make install完成后会在C:\cygwin\usr\local\include(Cygwin路径)下生成google文件


把生成的文件复制到你的NDK编译的交叉编译工具链的头文件中,我的路径为下图所示,供参考


4、新建.MK文件

新建个Android.mk文件,文件内容为:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
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-2.5.0
#LOCAL_CFLAGS := -std=c99
include $(BUILD_STATIC_LIBRARY)  

5、配置我们安卓项目中的文件

我项目的文件路径如下图,仅供参考


修改的具体项如下图:


AL_CPP_EXTENTION := .cc .cpp
LOCAL_WHOLE_STATIC_LIBRARIES += protobuf_static
$(call import-module,extensions) \
$(call import-module,external/protobuf-2.5.0) 

注意文件夹名

6、新建和修改config.h文件

在下面路径中新建文件


文件内容如下:

/* protobuf config.h for MSVC.  On other platforms, this is generated  
 * automatically by autoheader / autoconf / configure. */  
  
/* the location of <hash_map> */  
//#define HASH_MAP_H <hash_map>  
  
/* the namespace of hash_map/hash_set */  
// Apparently Microsoft decided to move hash_map *back* to the std namespace  
// in MSVC 2010:  
//   http://blogs.msdn.com/vcblog/archive/2009/05/25/stl-breaking-changes-in-visual-studio-2010-beta-1.aspx  
// TODO(kenton):  Use unordered_map instead, which is available in MSVC 2010.  
#if _MSC_VER < 1310 || _MSC_VER >= 1600  
#define HASH_NAMESPACE std  
#else  
#define HASH_NAMESPACE stdext  
#endif  
  
/* the location of <hash_set> */  
#define HASH_SET_H <ext/hash_set>  
#define HASH_MAP_H <ext/hash_map>  
//#define HASH_NAMESPACE _gnu_cxx  
  
/* define if the compiler has hash_map */  
//#define HAVE_HASH_MAP 1  
  
/* define if the compiler has hash_set */  
//#define HAVE_HASH_SET 1  
  
#define HAVE_PTHREAD 1  
  
/* define if you want to use zlib.  See readme.txt for additional  
 * requirements. */  
// #define HAVE_ZLIB 1 


7、用Cygwin编译

我用Cygwin编译的过程中出现问题了,错误类型基本一样,但修改比较麻烦,错误如下图:


错误是文件路径错误,我的修改方法为添加src/


对于#include “config.h”的引用需添加为:#include “src/google/protobuf/config.h”

这么做不好的地方在于要修改很多,估计你得修改40分钟左右,用Cygwin编译修改,很麻烦,好处是改完后编译通过了,打包到安卓设备上时也能用。

我现在是不明白他们之间的路径关系,读者看到的话,可以尝试避免这个麻烦的步骤。

OK,写完手工~













  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值