1

I'm trying to get Protobuf lib to communicate between Java And JNI layer in Android. I get source code guided from here. So I added this as source file in Jni>Source_Sirectory. Now if I perform ndk-build to generate .so file then it prompt following error Log:

[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

Any suggestion if I'm missing anything here?

CoDe
  • 11,056
  • 14
  • 90
  • 197

1 Answers1

2

Proto buffer generated c/c++ code is dependent on google's support code, which is installed alongside the proto buffer compiler. This means all the c/c++ you get is dependent on protobuf's support code, which is why you get linkage errors - apparently you did not link the google's proto buffer support library alongside your other linked resources when generating your shared object.

I created an eclipse build for JNI .so libraries, where I added a -lprotobuf flag to the linker, as can be seen here (search for the makefile, then scroll to the Linker section). Hope this help.

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
  • It looks like he's actually trying to build the support library itself. – Kenton Varda Sep 22 '15 at 04:32
  • @KentonVarda afaik, It require lib(libprotobuf-lite.so) to build Protobuf related source code that we write. But as I did not found .so file so I tried to build it my self. But [this V2.6.1](https://github.com/julienr/protobuf-android) worked me to generate lib. Now the only issue, my system run on V3.0.0, so proto files **.h** and **.cc** get generate using V3.0.0 but supported .so file has old version i.e. 2.6.1. So mismatch occur. Any suggestion here! – CoDe Sep 22 '15 at 05:45
  • @Shubn Then the solution here should be simple enough : make sure you use the same protobuf version for both the protobuf support code and the protobuf compiler. I'd suggest removing your currently installed protobuf version, installing the version you wish to use, and recompiling your protofiles, with linkage to the protbuf .so support library. The fact that your system uses one proto version and you apparently also have another proto version for proto compiling is a sure path to a world of pain ;) – Rann Lifshitz Sep 22 '15 at 07:49
  • @Shubh: You definitely can't combine headers from one version with a library built at a different version -- protobuf does not provide any ABI compatibility between library versions. – Kenton Varda Sep 23 '15 at 03:57
  • @RannLifshitz, Kenton. Thanks that's true so the way things mention [here @V2.6.1](https://github.com/julienr/protobuf-android) I update code of V3.0.0 and build for android. So now my sytsem and code to build .so file hasve same version. Now I'm facing some other issue mention [here](http://stackoverflow.com/a/10392475/2624806). Any suggestion there ? – CoDe Sep 23 '15 at 05:03
  • @Shubn : undefined 'pthread.....' ? In my eclipse setting I just added -pthread to my linker flags, and you can try linking boost's pthread library as seen here: http://stackoverflow.com/questions/6919534/problem-linking-to-boost-thread – Rann Lifshitz Sep 23 '15 at 06:43
  • @RannLifshitz Thanks, That has solved just by use same version at both side. Running issue is different I mentioned in comments [here](http://stackoverflow.com/a/10392475/2624806). Please check. – CoDe Sep 23 '15 at 07:02
  • @Shubn It is unclear what your current problem is. Can you explicitly state what currently prevents you from using your protobuf solution ? – Rann Lifshitz Sep 23 '15 at 07:42
  • @RannLifshitz join chat room [here](http://chat.stackoverflow.com/rooms/90404/protobuf-for-android-ndk) we can continue discuss here. – CoDe Sep 23 '15 at 08:22