[Android NDK]windows下使用Android编译protobuf 3.0

hfrommane 2016-04-05

原文:http://transing.xyz/2015/07/23/build-protobuf-with-ndk-under-windows/

git clone https://github.com/google/protobuf.git
git checkout v3.0.0-alpha-3.1

在protobuf文件夹下建立build.bat,内容如下

set PATH=%PATH%;E:\devel\android-ndk-r9d
ndk-build.cmd
pause

在protobuf文件夹下新建jni文件夹,然后新建Android.mk文件,里面内容如如下(这里的内容主要是参考src/Makefile.am文件得到的)

# Copyright 2006 The Android Open Source Project

LOCAL_PATH := $(call my-dir)  
SRC_PATH := ../src

include $(CLEAR_VARS)

LOCAL_CFLAGS:= -DHAVE_PTHREAD=1

LOCAL_SRC_FILES:= \
    $(SRC_PATH)/google/protobuf/stubs/atomicops_internals_x86_gcc.cc         \
    $(SRC_PATH)/google/protobuf/stubs/atomicops_internals_x86_msvc.cc        \
    $(SRC_PATH)/google/protobuf/stubs/common.cc                              \
    $(SRC_PATH)/google/protobuf/stubs/once.cc                                \
    $(SRC_PATH)/google/protobuf/stubs/stringprintf.cc                        \
    $(SRC_PATH)/google/protobuf/arena.cc                                     \
    $(SRC_PATH)/google/protobuf/arenastring.cc                               \
    $(SRC_PATH)/google/protobuf/extension_set.cc                             \
    $(SRC_PATH)/google/protobuf/generated_message_util.cc                    \
    $(SRC_PATH)/google/protobuf/message_lite.cc                              \
    $(SRC_PATH)/google/protobuf/repeated_field.cc                            \
    $(SRC_PATH)/google/protobuf/wire_format_lite.cc                          \
    $(SRC_PATH)/google/protobuf/io/coded_stream.cc                           \
    $(SRC_PATH)/google/protobuf/io/zero_copy_stream.cc                       \
    $(SRC_PATH)/google/protobuf/io/zero_copy_stream_impl_lite.cc \
    $(SRC_PATH)/google/protobuf/any.pb.cc                                    \
    $(SRC_PATH)/google/protobuf/api.pb.cc                                    \
    $(SRC_PATH)/google/protobuf/any.cc                                       \
    $(SRC_PATH)/google/protobuf/descriptor.cc                                \
    $(SRC_PATH)/google/protobuf/descriptor_database.cc                       \
    $(SRC_PATH)/google/protobuf/descriptor.pb.cc                             \
    $(SRC_PATH)/google/protobuf/duration.pb.cc                               \
    $(SRC_PATH)/google/protobuf/dynamic_message.cc                           \
    $(SRC_PATH)/google/protobuf/empty.pb.cc                                  \
    $(SRC_PATH)/google/protobuf/extension_set_heavy.cc                       \
    $(SRC_PATH)/google/protobuf/field_mask.pb.cc                             \
    $(SRC_PATH)/google/protobuf/generated_message_reflection.cc              \
    $(SRC_PATH)/google/protobuf/map_field.cc                                 \
    $(SRC_PATH)/google/protobuf/message.cc                                   \
    $(SRC_PATH)/google/protobuf/reflection_ops.cc                            \
    $(SRC_PATH)/google/protobuf/service.cc                                   \
    $(SRC_PATH)/google/protobuf/source_context.pb.cc                         \
    $(SRC_PATH)/google/protobuf/struct.pb.cc                                 \
    $(SRC_PATH)/google/protobuf/stubs/structurally_valid.cc                  \
    $(SRC_PATH)/google/protobuf/stubs/strutil.cc                             \
    $(SRC_PATH)/google/protobuf/stubs/substitute.cc                          \
    $(SRC_PATH)/google/protobuf/text_format.cc                               \
    $(SRC_PATH)/google/protobuf/timestamp.pb.cc                              \
    $(SRC_PATH)/google/protobuf/type.pb.cc                                   \
    $(SRC_PATH)/google/protobuf/unknown_field_set.cc                         \
    $(SRC_PATH)/google/protobuf/wire_format.cc                               \
    $(SRC_PATH)/google/protobuf/wrappers.pb.cc                               \
    $(SRC_PATH)/google/protobuf/io/gzip_stream.cc                            \
    $(SRC_PATH)/google/protobuf/io/printer.cc                                \
    $(SRC_PATH)/google/protobuf/io/strtod.cc                                 \
    $(SRC_PATH)/google/protobuf/io/tokenizer.cc                              \
    $(SRC_PATH)/google/protobuf/io/zero_copy_stream_impl.cc                  \
    $(SRC_PATH)/google/protobuf/compiler/importer.cc                         \
    $(SRC_PATH)/google/protobuf/compiler/parser.cc

LOCAL_C_INCLUDES:= \
    $(LOCAL_PATH)/../src \
    $(LOCAL_PATH)/../

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -lz
LOCAL_SHARED_LIBRARIES:= 
LOCAL_MODULE:= protobuf
LOCAL_MODULE_TAGS := optional

include $(BUILD_SHARED_LIBRARY)

  

再建立Application.mk文件,内容如下

APP_MODULES      := protobuf
APP_PLATFORM     := android-9
APP_ABI          := armeabi-v7a
APP_STL          := gnustl_static
APP_OPTIM        := release
 
APP_CPPFLAGS     := -fexceptions -frtti

过程中会提示缺少config.h和pbconfig.h,这两个文件是跑configure后才生成,从linux编译一次protobuf生成后拷贝到windows就好了。

或者自己手动建立,config.h的内容如下

/* config.h.  Generated from config.h.in by configure.  */
/* config.h.in.  Generated from configure.ac by autoheader.  */
 
/* the name of <hash_map> */
#define HASH_MAP_CLASS unordered_map
 
/* the location of <unordered_map> or <hash_map> */
#define HASH_MAP_H <tr1/unordered_map>
 
/* the namespace of hash_map/hash_set */
#define HASH_NAMESPACE std::tr1
 
/* the name of <hash_set> */
#define HASH_SET_CLASS unordered_set
 
/* the location of <unordered_set> or <hash_set> */
#define HASH_SET_H <tr1/unordered_set>
 
/* Define to 1 if you have the <dlfcn.h> header file. */
#define HAVE_DLFCN_H 1
 
/* Define to 1 if you have the <fcntl.h> header file. */
#define HAVE_FCNTL_H 1
 
/* Define to 1 if you have the `ftruncate' function. */
#define HAVE_FTRUNCATE 1
 
/* 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 to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1
 
/* Define to 1 if you have the <limits.h> header file. */
#define HAVE_LIMITS_H 1
 
/* Define to 1 if you have the <memory.h> header file. */
#define HAVE_MEMORY_H 1
 
/* Define to 1 if you have the `memset' function. */
#define HAVE_MEMSET 1
 
/* Define to 1 if you have the `mkdir' function. */
#define HAVE_MKDIR 1
 
/* Define if you have POSIX threads libraries and header files. */
#define HAVE_PTHREAD 1
 
/* Define to 1 if you have the <stdint.h> header file. */
#define HAVE_STDINT_H 1
 
/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1
 
/* Define to 1 if you have the `strchr' function. */
#define HAVE_STRCHR 1
 
/* Define to 1 if you have the `strerror' function. */
#define HAVE_STRERROR 1
 
/* Define to 1 if you have the <strings.h> header file. */
#define HAVE_STRINGS_H 1
 
/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1
 
/* Define to 1 if you have the `strtol' function. */
#define HAVE_STRTOL 1
 
/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1
 
/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1
 
/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1
 
/* Enable classes using zlib compression. */
#define HAVE_ZLIB 1
 
/* Define to the sub-directory in which libtool stores uninstalled libraries.
   */
#define LT_OBJDIR ".libs/"
 
/* Name of package */
#define PACKAGE "protobuf"
 
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "[email protected]"
 
/* Define to the full name of this package. */
#define PACKAGE_NAME "Protocol Buffers"
 
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "Protocol Buffers 3.0.0-alpha-3"
 
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "protobuf"
 
/* Define to the home page for this package. */
#define PACKAGE_URL ""
 
/* Define to the version of this package. */
#define PACKAGE_VERSION "3.0.0-alpha-3"
 
/* Define to necessary symbol if this constant uses a non-standard name on
   your system. */
/* #undef PTHREAD_CREATE_JOINABLE */
 
/* 64bit enabled */
/* #undef SOLARIS_64BIT_ENABLED */
 
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1
 
/* Enable extensions on AIX 3, Interix.  */
#ifndef _ALL_SOURCE
# define _ALL_SOURCE 1
#endif
/* Enable GNU extensions on systems that have them.  */
#ifndef _GNU_SOURCE
# define _GNU_SOURCE 1
#endif
/* Enable threading extensions on Solaris.  */
#ifndef _POSIX_PTHREAD_SEMANTICS
# define _POSIX_PTHREAD_SEMANTICS 1
#endif
/* Enable extensions on HP NonStop.  */
#ifndef _TANDEM_SOURCE
# define _TANDEM_SOURCE 1
#endif
/* Enable general extensions on Solaris.  */
#ifndef __EXTENSIONS__
# define __EXTENSIONS__ 1
#endif
 
 
/* Version number of package */
#define VERSION "3.0.0-alpha-3"
 
/* Define to 1 if on MINIX. */
/* #undef _MINIX */
 
/* Define to 2 if the system does not provide POSIX.1 features except with
   this defined. */
/* #undef _POSIX_1_SOURCE */
 
/* Define to 1 if you need to in order for `stat' and other things to work. */
/* #undef _POSIX_SOURCE */

pbconfig.h内容如下

#define GOOGLE_PROTOBUF_HASH_MAP_CLASS unordered_map
#define GOOGLE_PROTOBUF_HASH_MAP_H <tr1/unordered_map>
#define GOOGLE_PROTOBUF_HASH_NAMESPACE std::tr1
#define GOOGLE_PROTOBUF_HASH_SET_CLASS unordered_set
#define GOOGLE_PROTOBUF_HASH_SET_H <tr1/unordered_set>
#define GOOGLE_PROTOBUF_HAVE_HASH_MAP 1
#define GOOGLE_PROTOBUF_HAVE_HASH_SET 1

相关推荐