NDK编译protobuf-3.12.0-rc2

本文详细介绍如何使用Android NDK r13b编译protobuf 3.12.0-rc2版本,包括环境配置、Makefile设置及常见错误解决方法。

今天讲一下NDK编译protobuf-3.12.0-rc2:

下载android-ndk-r13b-windows-x86_64.zip:https://dl.google.com/android/repository/android-ndk-r13b-windows-x86_64.zip?utm_source=androiddevtools&utm_medium=website,解压到::C:/android/android-ndk-r13b

下载protobuf-3.12.0-rc2:https://github.com/protocolbuffers/protobuf/releases/,解压到:c:/protobuf-3.12.0-rc2

一。添加NDK环境变量:

我的NDK安装路径为:C:/android/android-ndk-r13b,因为在CMD下编译,要运行 ndk-build命令,所有要加2个系统环境变量:

cocos2d-x要的变量为:NDK_ROOT

ndk-build命令环境变量在Path中加:%NDK_ROOT%\build 

二.在c:/protobuf-3.12.0-rc2/src 建立make文件(文件名注意大小写):

1.application.mk

APP_MODULES      := protobuf
APP_PLATFORM     := android-19
APP_ABI          := armeabi-v7a
APP_STL := c++_static
APP_OPTIM        := release

2.android.mk

LOCAL_PATH := $(call my-dir)
 
include $(CLEAR_VARS)
 
LOCAL_MODULE := protobuf
LOCAL_CFLAGS := -std=c++11 -fexceptions -frtti
LOCAL_MODULE_FILENAME := libprotobuf
LOCAL_SRC_FILES :=  google/protobuf/stubs/bytestream.cc                          \
  google/protobuf/stubs/bytestream.h                           \
  google/protobuf/stubs/common.cc                              \
  google/protobuf/stubs/hash.h                                 \
  google/protobuf/stubs/int128.cc                              \
  google/protobuf/stubs/int128.h                               \
  google/protobuf/io/io_win32.cc                               \
  google/protobuf/stubs/map_util.h                             \
  google/protobuf/stubs/mathutil.h                             \
  google/protobuf/stubs/status.cc                              \
  google/protobuf/stubs/status.h                               \
  google/protobuf/stubs/status_macros.h                        \
  google/protobuf/stubs/statusor.cc                            \
  google/protobuf/stubs/statusor.h                             \
  google/protobuf/stubs/stringpiece.cc                         \
  google/protobuf/stubs/stringpiece.h                          \
  google/protobuf/stubs/stringprintf.cc                        \
  google/protobuf/stubs/stringprintf.h                         \
  google/protobuf/stubs/structurally_valid.cc                  \
  google/protobuf/stubs/strutil.cc                             \
  google/protobuf/stubs/time.cc                                \
  google/protobuf/stubs/time.h                                 \
  google/protobuf/any_lite.cc                                  \
  google/protobuf/arena.cc                                     \
  google/protobuf/extension_set.cc                             \
  google/protobuf/generated_enum_util.cc                       \
  google/protobuf/generated_message_util.cc                    \
  google/protobuf/generated_message_table_driven_lite.h        \
  google/protobuf/generated_message_table_driven_lite.cc       \
  google/protobuf/implicit_weak_message.cc                     \
  google/protobuf/message_lite.cc                              \
  google/protobuf/parse_context.cc                             \
  google/protobuf/repeated_field.cc                            \
  google/protobuf/wire_format_lite.cc                          \
  google/protobuf/io/coded_stream.cc                           \
  google/protobuf/io/strtod.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/any.pb.cc                                    \
  google/protobuf/api.pb.cc                                    \
  google/protobuf/any.cc                                       \
  google/protobuf/descriptor.cc                                \
  google/protobuf/descriptor_database.cc                       \
  google/protobuf/descriptor.pb.cc                             \
  google/protobuf/duration.pb.cc                               \
  google/protobuf/dynamic_message.cc                           \
  google/protobuf/empty.pb.cc                                  \
  google/protobuf/extension_set_heavy.cc                       \
  google/protobuf/field_mask.pb.cc                             \
  google/protobuf/generated_message_reflection.cc              \
  google/protobuf/generated_message_table_driven_lite.h        \
  google/protobuf/generated_message_table_driven.cc            \
  google/protobuf/map_field.cc                                 \
  google/protobuf/message.cc                                   \
  google/protobuf/reflection_internal.h                        \
  google/protobuf/reflection_ops.cc                            \
  google/protobuf/service.cc                                   \
  google/protobuf/source_context.pb.cc                         \
  google/protobuf/struct.pb.cc                                 \
  google/protobuf/stubs/substitute.cc                          \
  google/protobuf/stubs/substitute.h                           \
  google/protobuf/text_format.cc                               \
  google/protobuf/timestamp.pb.cc                              \
  google/protobuf/type.pb.cc                                   \
  google/protobuf/unknown_field_set.cc                         \
  google/protobuf/wire_format.cc                               \
  google/protobuf/wrappers.pb.cc                               \
  google/protobuf/io/gzip_stream.cc                            \
  google/protobuf/io/printer.cc                                \
  google/protobuf/io/tokenizer.cc                              \
  google/protobuf/compiler/importer.cc                         \
  google/protobuf/compiler/parser.cc                           \
  google/protobuf/util/delimited_message_util.cc               \
  google/protobuf/util/field_comparator.cc                     \
  google/protobuf/util/field_mask_util.cc                      \
  google/protobuf/util/internal/constants.h                    \
  google/protobuf/util/internal/datapiece.cc                   \
  google/protobuf/util/internal/datapiece.h                    \
  google/protobuf/util/internal/default_value_objectwriter.cc  \
  google/protobuf/util/internal/default_value_objectwriter.h   \
  google/protobuf/util/internal/error_listener.cc              \
  google/protobuf/util/internal/error_listener.h               \
  google/protobuf/util/internal/expecting_objectwriter.h       \
  google/protobuf/util/internal/field_mask_utility.cc          \
  google/protobuf/util/internal/field_mask_utility.h           \
  google/protobuf/util/internal/json_escaping.cc               \
  google/protobuf/util/internal/json_escaping.h                \
  google/protobuf/util/internal/json_objectwriter.cc           \
  google/protobuf/util/internal/json_objectwriter.h            \
  google/protobuf/util/internal/json_stream_parser.cc          \
  google/protobuf/util/internal/json_stream_parser.h           \
  google/protobuf/util/internal/location_tracker.h             \
  google/protobuf/util/internal/mock_error_listener.h          \
  google/protobuf/util/internal/object_location_tracker.h      \
  google/protobuf/util/internal/object_source.h                \
  google/protobuf/util/internal/object_writer.cc               \
  google/protobuf/util/internal/object_writer.h                \
  google/protobuf/util/internal/protostream_objectsource.cc    \
  google/protobuf/util/internal/protostream_objectsource.h     \
  google/protobuf/util/internal/protostream_objectwriter.cc    \
  google/protobuf/util/internal/protostream_objectwriter.h     \
  google/protobuf/util/internal/proto_writer.cc                \
  google/protobuf/util/internal/proto_writer.h                 \
  google/protobuf/util/internal/structured_objectwriter.h      \
  google/protobuf/util/internal/type_info.cc                   \
  google/protobuf/util/internal/type_info.h                    \
  google/protobuf/util/internal/type_info_test_helper.cc       \
  google/protobuf/util/internal/type_info_test_helper.h        \
  google/protobuf/util/internal/utility.cc                     \
  google/protobuf/util/internal/utility.h                      \
  google/protobuf/util/json_util.cc                            \
  google/protobuf/util/message_differencer.cc                  \
  google/protobuf/util/time_util.cc                            \
  google/protobuf/util/type_resolver_util.cc

 
 
LOCAL_EXPORT_C_INCLUDES :=
LOCAL_EXPORT_LDLIBS :=
 
LOCAL_C_INCLUDES := $(LOCAL_PATH) \
$(LOCAL_PATH)/src
 
LOCAL_LDLIBS := -llog -lz
 
#include $(BUILD_SHARED_LIBRARY)
include $(BUILD_STATIC_LIBRARY)

 

里面包含的文件很多,其实要包含的文件都在Makefile.am文件中:

有两个为 libprotobuf_lite_la_SOURCES 的文件全部包含进来。还要注意的是,我的APP_PLATFORM     := android-19,必须要下载 android-19 SDK,Android  SDK 可在https://www.androiddevtools.cn/index.html下载。

准备好上面现在开始编译:

Win+R  执行 cmd命令,进入c:/protobuf-3.12.0-rc2/src目录 输入:

ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./android.mk NDK_APPLICATION_MK=./application.mk

 

会出现以下错误:

 

这个要修改一下C:/protobuf-3.12.0-rc2/src/google/protobuf/stubs/common.h文件就行了

打开加入下面内容:

#ifndef HAVE_PTHREAD
#define HAVE_PTHREAD
#endif

 

再次执行上次命令,就可以编译了:

 

这样就编译好了!

<think> 嗯,用户需要找到交叉编译protobuf-2.6.1的方法。根据之前的引用内容,特别是引用[4]给出了非常具体的交叉编译配置示例。用户明确提到版本号是2.6.1,这很关键,因为不同版本的protobuf编译配置可能有差异。 用户提供的引用中,引用[4]展示了针对海思平台的交叉编译命令,其中包含了几个重要参数:--host指定目标平台,CC和CXX指定交叉编译器,--with-protoc指定主机protoc路径,--prefix指定安装目录。这些参数是交叉编译的关键要素。 引用[2]也提到"我们一般使用2.6.1",说明这个版本在特定场景下被广泛采用。引用[3]则提供了交叉编译时的实用技巧,比如单独编译某个包的方法,这对调试编译问题很有帮助。 考虑到用户可能是在嵌入式环境使用,引用[5]提到的ARM指令模式选项也值得注意,虽然protobuf本身可能不需要特别设置这个参数。用户没有说明具体目标平台,所以准备在回答中强调需要替换工具链路径和平台标识符。 在回答时,应该突出几个核心步骤:准备主机protoc、配置交叉编译参数、解决可能的依赖问题。用户可能遇到的常见问题包括:主机protoc版本不匹配、交叉编译器路径错误、以及make install时的权限问题,这些都需要在回答中提醒。 </think> 根据引用内容,以下是交叉编译 protobuf-2.6.1 的关键步骤和注意事项: --- ### **交叉编译步骤** 1. **准备主机环境** - 在主机上安装与目标平台**匹配的交叉编译工具链**(如 `arm-linux-gnueabihf-gcc`) - 下载 [protobuf-2.6.1 源码](https://github.com/protocolbuffers/protobuf/releases/tag/v2.6.1) 2. **编译主机版 protoc**(必需) ```bash cd protobuf-2.6.1 ./configure --prefix=/path/to/host_protobuf # 指定主机安装路径 make && make install # 生成主机可用的 protoc 编译器 ``` 3. **配置交叉编译参数**(关键步骤) ```bash ./configure \ --host=arm-linux \ # 目标平台(替换为实际架构,如 aarch64-linux) CC=arm-hisiv200-linux-gcc \ # 交叉编译的 C 编译器 CXX=arm-hisiv200-linux-g++ \ # 交叉编译的 C++ 编译--with-protoc=/path/to/host_protobuf/bin/protoc # 指向主机编译protoc --prefix=/path/to/target_install # 目标平台输出路径 ``` > 📌 注意: > - `--host` 需根据目标芯片架构修改(如 `armv7a-linux`,`aarch64-linux`) > - `CC/CXX` 替换为实际交叉工具链路径 > - `--with-protoc` 必须指向**主机编译protoc**(引用[^4]) 4. **编译与安装** ```bash make -j$(nproc) # 并行编译 make install # 安装到 --prefix 指定路径 ``` --- ### **常见问题解决** 1. **动态库路径问题** 编译依赖 protobuf 的应用时,需通过 `link_directories()` 添加目标库路径(引用[^3]): ```cmake link_directories(/path/to/target_install/lib) ``` 2. **ARM指令集指定** 若需强制 32 位 ARM 指令(非 Thumb 模式),在编译应用时添加: ```makefile LOCAL_ARM_MODE := arm # Android NDK 或嵌入式场景(引用[^5]) ``` 3. **单独编译验证** 调试时建议单独编译以减少干扰: ```bash catkin_make --only-pkg-with-deps protobuf_catkin # ROS 场景(引用[^3]) ``` --- ### **验证安装** 1. **版本检查** ```bash # 在目标设备运行 /path/to/target_install/bin/protoc --version ``` 2. **编译测试 .proto 文件** ```bash protoc --proto_path=. --cpp_out=. test.proto # 生成 C++ 代码 ``` --- ### 相关问题 1. 为什么交叉编译 protobuf 必须先在主机编译 protoc? 2. 如何为不同 ARM 架构(如 Cortex-A53 和 Cortex-M7)调整交叉编译参数? 3. 交叉编译后运行时出现 `GLIBCXX` 版本错误如何解决? 4. 在 Yocto 或 Buildroot 中如何集成 protobuf-2.6.1 的交叉编译? > 提示:实际工具链名称需匹配目标平台(如海思平台用 `arm-hisivXXX-linux-gcc`),可通过 `find / -name "*gcc"` 搜索工具链路径。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值