Android的NDK和使用dlopen动态加载库 [英] Android ndk and dynamic load library with dlopen

查看:2623
本文介绍了Android的NDK和使用dlopen动态加载库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从外部文件目录动态加载共享库。

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

创建库并测试它在Linux上,它的工作原理。
编译:GCC -fPIC -shared -static-的libstdc ++ -static-libgcc的test_lib.cpp constructor.cpp -o test.so

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

但是,当我在Android中使用NDK此,我得到运行时错误:
的dlopen失败:图书馆libc.so.6的找不到。

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

推荐答案

您不能使用在Android上正常的Linux内置的库 - 它仍然链接到共享 libc.so.6的这是不是可以在Android。 (你不能静态链接的共享库,例如见<一href=\"http://stackoverflow.com/a/6637842/3115956\">http://stackoverflow.com/a/6637842/3115956.)

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. http://stackoverflow.com/a/6637842/3115956.)

您需要使用针对Android的C运行时库的NDK编译器来构建库。

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

如果你喜欢冒险,你可以尝试迫使它的C运行时虽然静态链接到库中,通过的<$ c将其链接的libc.a 代替$ C> libc.so ,但我不知道这是否会导致其他问题。 (一个可以运行在Android上正常的Linux内置的可执行文件,假设他们正在与 -static 建的,但我不知道如何,对于共享库的工作原理。)

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.)

这篇关于Android的NDK和使用dlopen动态加载库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆