4

i want to load shared library dynamically from external files directory.

I create library and test it on linux, it works. Compile: gcc -fPIC -shared -static-libstdc++ -static-libgcc test_lib.cpp constructor.cpp -o test.so

But when i use this in android ndk, i get runtime error: dlopen failed: library "libc.so.6" not found.

How i can solve it? I'm use emulator

Sinigr
  • 154
  • 1
  • 11

1 Answers1

2

You can't use libraries built for normal linux on android - it's still linking to the shared libc.so.6 which isn't available on Android. (You can't statically link a shared library, see e.g. https://stackoverflow.com/a/6637842/3115956.)

You need to build the library using the NDK compiler against the Android C runtime library.

If you're adventurous, you could try forcing it to link the C runtime statically into your library though, by making it link libc.a instead of libc.so, but I'm not sure if this causes other issues. (One can run executables built for normal linux on android, assuming they're built with -static, but I'm not sure how well that works for shared libraries.)

Community
  • 1
  • 1
mstorsjo
  • 12,983
  • 2
  • 39
  • 62
  • The first part of your answer is good, but I see no reason to force linking the Android C runtime. That is bad advice when it is easier and safer to actually compile the library for the platform you are targeting. – Francesca Nannizzi Jun 02 '16 at 13:02
  • I'm not sure I understand - ideally he should be building his library for the platform he's targeting - I agree. When wanting to run it on android, that would be done by using the android NDK, which would make sure it's linked against the android C runtime and not against glibc. – mstorsjo Jun 02 '16 at 14:02
  • "You can't use libraries built for normal linux on android - it's still linking to the shared libc.so.6 which isn't available on Android." But for example boost works in ndk. Why? – Sinigr Jun 02 '16 at 16:11
  • I'm not sure about boost, either you're referring to parts of it that only use headers, not a precompiled .so, or that .so doesn't link to anything in glibc? – mstorsjo Jun 02 '16 at 18:29