`
aigo
  • 浏览: 2538043 次
  • 性别: Icon_minigender_1
  • 来自: 宜昌
社区版块
存档分类
最新评论

cygwin中编译基于Android NDK的protobuf2.6静态库

阅读更多

原文作者:@玄冬Wong

以下构建配置在protobuf 2.6.1测试通过,最新的3.x没试过。

另外,这里构建的是移动平台版本protobuf-lite,不是protobuf

 

1,cygwin安装需要的lib库

cygwin中编译protobuf是比较麻烦的,因为很多必要的库都没安装,我安装一遍下来,发现一下lib是必须安装的,否则安装不成功:

wget 

bzip2

curl

autoconf

automake

libtool

make

gcc

 

这些库在cygwin的安装选项中勾选,如果搜索出来的结果有多个,建议一定勾上不带版本后缀的名字。比如你在搜索automake,会出现多个结果:automake、automake-1.8、automake-1.9等等,这里建议一定勾上不带版本后缀automake。

这里给一个配置好的cygwin,上述lib已全部装好,解压后可以直接使用。

https://yunpan.cn/cqnEtCkDtDPkv  访问密码 e0f1

如果是在linux上编译,那么只需要按照这篇文章上的步骤安装lib即可:

http://bafeimao.net/2015/11/14/compile-protobuf-on-mac/

 

2,获取config.h

上述cygwin安装好以后,切换到protobuf的源码目录下,执行命令:

./autogen.sh

 

然后执行:

./configure

 

如果没有错误,那么根目录下会生成一个C++头文件:config.h。这个文件是后面编译protobuf时需要的头文件。如果没有这个config.h文件,那么后面编译时protobuf时会报错:error "No suitable threading library available."

 

这里附上2.6.1的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. */
/* #undef HAVE_ZLIB */

/* Define to the sub-directory where 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 "protobuf@googlegroups.com"

/* 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 2.6.1"

/* 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 "2.6.1"

/* 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 "2.6.1"

/* 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 */

  

 

3,编写Android.mk

新建Application.mk和Android.mk两个配置文件,配置文件内容如下:

Application.mk 

APP_STL := gnustl_static
APP_ABI := armeabi-v7a armeabi
APP_PROJECT_PATH := ./
APP_BUILD_SCRIPT := ./jni/Android.mk

 

Android.mk 

# Copyright (C) 2009 The Android Open Source Project  
#  
# Licensed under the Apache License, Version 2.0 (the "License");  
# you may not use this file except in compliance with the License.  
# You may obtain a copy of the License at  
#  
#      http://www.apache.org/licenses/LICENSE-2.0  
#  
# Unless required by applicable law or agreed to in writing, software  
# distributed under the License is distributed on an "AS IS" BASIS,  
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
# See the License for the specific language governing permissions and  
# limitations under the License.  
#  
#  
  
LOCAL_PATH := $(call my-dir)  
include $(CLEAR_VARS)  
CC_LITE_SRC_FILES := 										\
google/protobuf/stubs/common.cc								\
google/protobuf/stubs/once.cc                                \
google/protobuf/extension_set.cc                             \
google/protobuf/generated_message_util.cc                    \
google/protobuf/message_lite.cc                              \
google/protobuf/repeated_field.cc                            \
google/protobuf/wire_format_lite.cc                          \
google/protobuf/io/coded_stream.cc                           \
google/protobuf/io/zero_copy_stream.cc                       \
google/protobuf/io/zero_copy_stream_impl_lite.cc  
  
  
COMPILER_SRC_FILES :=  \
google/protobuf/descriptor.cc \
google/protobuf/descriptor.pb.cc \
google/protobuf/descriptor_database.cc \
google/protobuf/dynamic_message.cc \
google/protobuf/extension_set.cc \
google/protobuf/extension_set_heavy.cc \
google/protobuf/generated_message_reflection.cc \
google/protobuf/generated_message_util.cc \
google/protobuf/message.cc \
google/protobuf/message_lite.cc \
google/protobuf/reflection_ops.cc \
google/protobuf/repeated_field.cc \
google/protobuf/service.cc \
google/protobuf/text_format.cc \
google/protobuf/unknown_field_set.cc \
google/protobuf/wire_format.cc \
google/protobuf/wire_format_lite.cc \
google/protobuf/compiler/code_generator.cc \
google/protobuf/compiler/command_line_interface.cc \
google/protobuf/compiler/importer.cc \
google/protobuf/compiler/main.cc \
google/protobuf/compiler/parser.cc \
google/protobuf/compiler/plugin.cc \
google/protobuf/compiler/plugin.pb.cc \
google/protobuf/compiler/subprocess.cc \
google/protobuf/compiler/zip_writer.cc \
google/protobuf/compiler/cpp/cpp_enum.cc \
google/protobuf/compiler/cpp/cpp_enum_field.cc \
google/protobuf/compiler/cpp/cpp_extension.cc \
google/protobuf/compiler/cpp/cpp_field.cc \
google/protobuf/compiler/cpp/cpp_file.cc \
google/protobuf/compiler/cpp/cpp_generator.cc \
google/protobuf/compiler/cpp/cpp_helpers.cc \
google/protobuf/compiler/cpp/cpp_message.cc \
google/protobuf/compiler/cpp/cpp_message_field.cc \
google/protobuf/compiler/cpp/cpp_primitive_field.cc \
google/protobuf/compiler/cpp/cpp_service.cc \
google/protobuf/compiler/cpp/cpp_string_field.cc \
google/protobuf/compiler/java/java_enum.cc \
google/protobuf/compiler/java/java_enum_field.cc \
google/protobuf/compiler/java/java_extension.cc \
google/protobuf/compiler/java/java_field.cc \
google/protobuf/compiler/java/java_file.cc \
google/protobuf/compiler/java/java_generator.cc \
google/protobuf/compiler/java/java_helpers.cc \
google/protobuf/compiler/java/java_message.cc \
google/protobuf/compiler/java/java_message_field.cc \
google/protobuf/compiler/java/java_primitive_field.cc \
google/protobuf/compiler/java/java_service.cc \
google/protobuf/compiler/javamicro/javamicro_enum.cc \
google/protobuf/compiler/javamicro/javamicro_enum_field.cc \
google/protobuf/compiler/javamicro/javamicro_field.cc \
google/protobuf/compiler/javamicro/javamicro_file.cc \
google/protobuf/compiler/javamicro/javamicro_generator.cc \
google/protobuf/compiler/javamicro/javamicro_helpers.cc \
google/protobuf/compiler/javamicro/javamicro_message.cc \
google/protobuf/compiler/javamicro/javamicro_message_field.cc \
google/protobuf/compiler/javamicro/javamicro_primitive_field.cc \
google/protobuf/compiler/python/python_generator.cc \
google/protobuf/io/coded_stream.cc \
google/protobuf/io/gzip_stream.cc \
google/protobuf/io/printer.cc \
google/protobuf/io/tokenizer.cc \
google/protobuf/io/zero_copy_stream.cc \
google/protobuf/io/zero_copy_stream_impl.cc \
google/protobuf/io/zero_copy_stream_impl_lite.cc \
google/protobuf/stubs/common.cc \
google/protobuf/stubs/once.cc \
google/protobuf/stubs/structurally_valid.cc \
google/protobuf/stubs/strutil.cc \
google/protobuf/stubs/substitute.cc  
  
# C++ full library  
# =======================================================  
#include $(CLEAR_VARS)  
  
LOCAL_MODULE := libprotobuf-lite  
LOCAL_MODULE_TAGS := optional  
  
LOCAL_CPP_EXTENSION := .cc  
  
LOCAL_SRC_FILES += \
$(CC_LITE_SRC_FILES)                                             #\
#google/protobuf/stubs/strutil.cc                             \
#google/protobuf/stubs/substitute.cc                          \
#google/protobuf/stubs/structurally_valid.cc                  \
#google/protobuf/descriptor.cc                                \
#google/protobuf/descriptor.pb.cc                             \
#google/protobuf/descriptor_database.cc                       \
#google/protobuf/dynamic_message.cc                           \
#google/protobuf/extension_set_heavy.cc                       \
#google/protobuf/generated_message_reflection.cc              \
#google/protobuf/message.cc                                   \
#google/protobuf/reflection_ops.cc                            \
#google/protobuf/service.cc                                   \
#google/protobuf/text_format.cc                               \
#google/protobuf/unknown_field_set.cc                         \
#google/protobuf/wire_format.cc                               \
#google/protobuf/io/gzip_stream.cc                            \
#google/protobuf/io/printer.cc                                \
#google/protobuf/io/tokenizer.cc                              \
#google/protobuf/io/zero_copy_stream_impl.cc                  \
#google/protobuf/compiler/importer.cc                         \
#google/protobuf/compiler/parser.cc

  
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/src  
  
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/android \
bionic \
$(LOCAL_PATH)/src \
$(JNI_H_INCLUDE)  
  
LOCAL_SHARED_LIBRARIES := \
libz libcutils libutils  
LOCAL_LDLIBS := -lz  
# stlport conflicts with the host stl library  
#ifneq ($(TARGET_SIMULATOR),true)  
#LOCAL_C_INCLUDES += external/stlport/stlport  
#LOCAL_SHARED_LIBRARIES += libstlport  
#endif  
  
# Define the header files to be copied  
#LOCAL_COPY_HEADERS := \
#    google/protobuf/stubs/once.h \
#    google/protobuf/stubs/common.h \
#    google/protobuf/io/coded_stream.h \
#    google/protobuf/generated_message_util.h \
#    google/protobuf/repeated_field.h \
#    google/protobuf/extension_set.h \
#    google/protobuf/wire_format_lite_inl.h  
#  
#LOCAL_COPY_HEADERS_TO := $(LOCAL_MODULE)  
  
LOCAL_CFLAGS := -march=armv7-a -mfloat-abi=softfp -DGOOGLE_PROTOBUF_NO_RTTI     #-DGOOGLE_PROTOBUF_NO_RTTI  
  
include $(BUILD_STATIC_LIBRARY)  

 

 Android.mk的配置参考自
http://stackoverflow.com/questions/7144008/how-to-build-protocol-buffer-by-android-ndk

 

4,设置目录结构

新建一个文件夹,比如:ProtobufBuild,然后在建一个子目录,名字叫jni这个名字不能任意,必须叫jni)。将Application.mk和Android.mk两个文件放在ProtobufBuild/jni/目录下,并且将protobuf的源码拷贝到ProtobufBuild/jni/目录下,并且将上面生成的config.h文件也拷贝到这个目录下,最终的文件目录结构是这样的:

ProtobufBuild

         |——jni

                  |——Application.mk

                  |——Android.mk

                  |——config.h

                  |——google(protobuf源码下载解压后,将protobuf/src/google拷贝过来的)

 

5,NDK命令行编译

将android-ndk的根目录配置到系统的环境变量中,然后将打开cmd,切换目录到到上面的新建的文件夹ProtobufBuild目录下,然后执行:

ndk-build

 无需参数,直接回车即可,最终生成的protobuf静态库文件出现在:

ProtobufBuild/obj/local/armeabi-v7a/libprotobuf-lite.a

 

注:armeabi-v7a是Application.mk中配置的CPU型号,这个配置怎么用具体不清楚,貌似当年android 2.2和2.3时代,这个cpu型号是最通用的吧,具体自己google。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics