如何构建 libprotobuf-lite.so

How to build libprotobuf-lite.so

我正在尝试让 Protobuf 库在 Java 和 Android 中的 JNI 层之间进行通信。我从 here 获得源代码指导。所以我在 Jni>Source_Sirectory 中添加了这个作为源文件。现在,如果我执行 ndk-build 生成 .so 文件,那么它会提示以下错误日志:

[armeabi] SharedLibrary  : libprotobuf-lite.so
jni/src/google/protobuf/stubs/common.cc:201: error: undefined reference to 'google::protobuf::util::Status::ToString() const'
jni/src/google/protobuf/stubs/common.cc:207: error: undefined reference to 'google::protobuf::operator<<(std::ostream&, google::protobuf::uint128 const&)'
jni/src/google/protobuf/arena.h:622: error: undefined reference to 'google::protobuf::Arena::AllocateAligned(std::type_info const*, unsigned int)'
jni/src/google/protobuf/arena.h:624: error: undefined reference to 'google::protobuf::Arena::AddListNode(void*, void (*)(void*))'
jni/src/google/protobuf/arena.h:462: error: undefined reference to 'google::protobuf::Arena::AddListNode(void*, void (*)(void*))'
jni/src/google/protobuf/arena.h:617: error: undefined reference to 'google::protobuf::Arena::AllocateAligned(std::type_info const*, unsigned int)'
jni/src/google/protobuf/arena.h:617: error: undefined reference to 'google::protobuf::Arena::AllocateAligned(std::type_info const*, unsigned int)'
jni/src/google/protobuf/arena.h:617: error: undefined reference to 'google::protobuf::Arena::AllocateAligned(std::type_info const*, unsigned int)'
jni/src/google/protobuf/arena.h:633: error: undefined reference to 'google::protobuf::Arena::AddListNode(void*, void (*)(void*))'
jni/src/google/protobuf/arena.h:624: error: undefined reference to 'google::protobuf::Arena::AddListNode(void*, void (*)(void*))'
jni/src/google/protobuf/wire_format_lite.cc:514: error: undefined reference to 'google::protobuf::internal::IsStructurallyValidUTF8(char const*, int)'
jni/src/google/protobuf/wire_format_lite.cc:527: error: undefined reference to 'google::protobuf::StringPrintf(char const*, ...)'
collect2: error: ld returned 1 exit status

如果我遗漏了什么,有什么建议吗?

Proto buffer 生成的 c/c++ 代码依赖于 google 的支持代码,它与 proto buffer 编译器一起安装。这意味着你得到的所有 c/c++ 都依赖于 protobuf 的支持代码,这就是你得到 linkage 错误的原因 - 显然你没有 link google 的生成共享对象时,proto 缓冲区支持库与其他 linked 资源一起使用。

我为 JNI .so 库创建了一个 eclipse 构建,我在其中向 linker 添加了一个 -lprotobuf 标志,可以看到 here (搜索 makefile,然后滚动到链接器部分)。 希望对您有所帮助。