protobuf/android 交叉编译笔记

本文详细介绍了如何在Windows平台上使用Android NDK工具链交叉编译protobuf库,使其能在Android arm-eabi-v7a架构上运行。主要内容包括环境搭建、cmake配置及编译过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

protobuf 交叉编译笔记

目标是使用 android ndk 的工具链编译出 android armeabi-v7a 可用的 protobuf 库。

交叉编译环境配置

windows 平台

  1. 下载 NDK x86_64 或者 NDK x86 并解压缩
  2. 下载 protobuf 源代码。这里以 protobuf 3.4.1 举例。
  3. 下载并安装最新的 cmake 3.10 x86_64 或者 cmake 3.10 x86

以上,准备完成。

使用 cmake 生成 makefile

选择一个你喜欢的位置,打开cmd,执行下述命令。

mkdir build
cd build
cmake -G "MinGW Makefiles" \
    -DCMAKE_TOOLCHAIN_FILE=<Your NDK PATH>/build/cmake/android.toolchain.cmake \
    -DCMAKE_MAKE_PROGRAM=<Your NDK PATH>/prebuilt/windows-x86_64/bin/make.exe \
    -Dprotobuf_BUILD_TESTS=OFF \
    <Protobuf source PATH>/cmake

几个关键参数略作解释。

CMAKE_TOOLCHAIN_FILE,这个变量指定了 cmake 使用的 toolchain 脚本,toolchain脚本会帮助配置交叉编译使用的头文件/库文件路径,配置编译器/链接器等一系列工具。

CMAKE_MAKE_PROGRAM,这个变量指定了cmake使用的make程序,如果系统的PATH中没有 make.exe 的话(通常也不会有,除非安装了 MinGW 并且配置了 PATH 环境变量),就需要指定使用的 make.exe 程序。这里使用了 ndk 自带的 prebuilt 版 make.exe

protobuf_BUILD_TESTS,不要编译测试用例,这一选项默认启用,但在交叉编译时会导致奇怪的问题。

-G "MinGW Makefiles",指定 cmake 生成 MinGW Makefile,即一般所说的 Makefile

编译

cmake --build . --target libprotobuf
cmake --build . --target libprotobuf-lite

分别编译libprotobuflibprotobuf-lite两个库,这两个库就是我们将会用到的 protobuf 静态库了。

转载于:https://www.cnblogs.com/UniqPtr/p/7859758.html

root@JFPC1:~/Workspace/protobuf-21.12# make -j$(nproc) make all-recursive make[1]: Entering directory '/root/Workspace/protobuf-21.12' Making all in . make[2]: Entering directory '/root/Workspace/protobuf-21.12' make[2]: Leaving directory '/root/Workspace/protobuf-21.12' Making all in src make[2]: Entering directory '/root/Workspace/protobuf-21.12/src' CXX google/protobuf/wire_format.lo CXX google/protobuf/wrappers.pb.lo CXX google/protobuf/compiler/command_line_interface.lo CXX google/protobuf/compiler/code_generator.lo CXX google/protobuf/compiler/plugin.lo CXX google/protobuf/compiler/plugin.pb.lo CXX google/protobuf/compiler/subprocess.lo CXX google/protobuf/compiler/zip_writer.lo CXX google/protobuf/any_lite.lo CXX google/protobuf/compiler/main.o CXX google/protobuf/arenastring.lo CXX google/protobuf/arena.lo CXX google/protobuf/arenaz_sampler.lo CXX google/protobuf/extension_set.lo CXX google/protobuf/generated_enum_util.lo CXX google/protobuf/generated_message_tctable_lite.lo CXX google/protobuf/generated_message_util.lo CXX google/protobuf/implicit_weak_message.lo CXX google/protobuf/inlined_string_field.lo CXX google/protobuf/io/coded_stream.lo CXX google/protobuf/io/io_win32.lo CXX google/protobuf/io/strtod.lo CXX google/protobuf/io/zero_copy_stream.lo CXX google/protobuf/io/zero_copy_stream_impl.lo CXX google/protobuf/io/zero_copy_stream_impl_lite.lo CXX google/protobuf/map.lo CXX google/protobuf/message_lite.lo CXX google/protobuf/parse_context.lo CXX google/protobuf/repeated_field.lo CXX google/protobuf/repeated_ptr_field.lo CXX google/protobuf/stubs/bytestream.lo CXX google/protobuf/stubs/common.lo CXX google/protobuf/stubs/int128.lo CXX google/protobuf/stubs/status.lo CXX google/protobuf/stubs/statusor.lo 这里报
03-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值