Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Building a program with protobuf results in undefined references #896

Closed
hhu006 opened this issue Jan 24, 2019 · 6 comments
Closed
Labels

Comments

@hhu006
Copy link

hhu006 commented Jan 24, 2019

Description

I am building a program which requires protobuf. The protobuf libraries are taken from the prebuilt protobuf libraries from Android 8.1 (libprotobuf-cpp-lite.so & libprotobuf-cpp-full.so). When building the program with the NDK standalone toolchain (android-ndk-r19), I encountered undefined reference errors as follows.

./../lib/libpi_core_items.a(item_msg.pb.cc.o): In function PI::proto::Mat::MergeFrom(PI::proto::Mat const&)': proto/item_msg.pb.cc:1524: undefined reference to google::protobuf::internal::ArenaStringPtr::AssignWithDefault(std::__ndk1::basic_string<char, std::__ndk1::char_traits, std::__ndk1::allocator > const*, google::protobuf::internal::ArenaStringPtr)'
proto/item_msg.pb.cc:1524: undefined reference to `google::protobuf::internal::ArenaStringPtr::AssignWithDefault(std::__ndk1::basic_string<char, std::__ndk1::char_traits, std::__ndk1::allocator > const*, google::protobuf::internal::ArenaStringPtr)'

The above function does exist in the protobuf libraries, however, the function symbol has a slight difference, e.g., the namespace of std::string is "std::__1::basic_string" instead of "std::__ndk1::basic_string" as shown below.

$ bin/aarch64-linux-android-objdump -CS libprotobuf-cpp-lite.so -T | grep AssignWithDefault
0000000000024180 g DF .text 0000000000000014 Base google::protobuf::internal::ArenaStringPtr::AssignWithDefault(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const*, google::protobuf::internal::ArenaStringPtr)
0000000000024180 <google::protobuf::internal::ArenaStringPtr::AssignWithDefault(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const*, google::protobuf::internal::ArenaStringPtr)@@base>:

Please advise why the NDK built program is not compatible with the Android prebuild libraries? In case of building a program which requires a library already existing in the Android tree, what will be the best way to link against it.

Environment Details

Not all of these will be relevant to every bug, but please provide as much
information as you can.

  • NDK Version: NDK r19
  • Build system: standalone toolchain
  • Host OS: Linux, Ubuntu 16.04
  • Compiler: Clang .
  • ABI: arm64-v8a
  • STL: libstdc++
  • NDK API level: 27
  • Device API level:
@hhu006 hhu006 added the bug label Jan 24, 2019
@enh
Copy link
Contributor

enh commented Jan 24, 2019

Please advise why the NDK built program is not compatible with the Android prebuild libraries?

because the libc++ in the version of the NDK you're using isn't necessarily the same as (ABI-compatible-with) the one in the version of the platform you're targeting.

In case of building a program which requires a library already existing in the Android tree, what will be the best way to link against it.

that's explicitly not supported. https://android-developers.googleblog.com/2016/06/improving-stability-with-private-cc.html

you'll need to build your own protobuf libraries.

@enh enh closed this as completed Jan 24, 2019
@hhu006
Copy link
Author

hhu006 commented Jan 24, 2019

Thanks very much for quick reply. However, if I build my own protobuf libraries, e.g., shared protobuf libraries, will it conflict with the shared protobuf libraries in Android?

@enh
Copy link
Contributor

enh commented Jan 24, 2019

it shouldn't be a problem.

if you're worried, you can always build them with a different soname (or just link all your libraries together).

@hhu006
Copy link
Author

hhu006 commented Jan 24, 2019

For example, if there is a function named A_foo in protobuf, since I have two sets of protobuf library in Android (one built by myself, the other from Android), even the libraries have distinct names, however, they will have the same function symbol for that function. When the program is executed, will the loader be confused to determine from which library the function will be called.

@enh
Copy link
Contributor

enh commented Jan 25, 2019

no, that system library is in a separate linker namespace.

@hhu006
Copy link
Author

hhu006 commented Jan 25, 2019

Thanks a lot. Good to know that. Will try our own library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants