首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.loadLibrary无法找到Android的GNU共享库

System.loadLibrary无法找到Android的GNU共享库
EN

Stack Overflow用户
提问于 2012-01-03 20:07:14
回答 6查看 14.3K关注 0票数 4

我用android和NDK编译了一个针对libstdc++的共享库,按照android-ndk-r7/docs/CPLUSPLUS-SUPPORT.html,我在加载库之前尝试加载gnustl_shared:

代码语言:javascript
复制
static {
    System.loadLibrary("gnustl_shared");
    System.loadLibrary("MathTest");
}

例如,我可以看到这里正在完成这一任务,但我得到了一个例外:

代码语言:javascript
复制
01-03 20:02:42.307: E/AndroidRuntime(569): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load gnustl_shared: findLibrary returned null

如果我不加载gnustl_shared,它就会失败,只有以下例外情况:

代码语言:javascript
复制
01-03 20:03:04.667: E/AndroidRuntime(603): Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: reloc_library[1311]:    33 cannot locate '_ZNSo3putEc'...

我用同样的问题在API级别8和9上进行了测试。我做错了什么?我假设它在最后一个异常中寻找的符号与STL相关,加载它将解决问题。是这种情况吗?

编辑:,我现在已经按照答案的建议包括了STL库。CMake生成的编译行是这样的(请注意,我的项目有三个文件:mother.c ( George的“所有随机数生成器可用的母亲”这里的副本)、Driver.cpp,它包含用于测试不同数学库和打印函数运行时的函数,以及包含JNI胶并从Driver.cpp调用测试函数的androidactivity.cpp。CMake生成的详细Makefile输出如下所示。我不确定这是不是有用的信息,但是

代码语言:javascript
复制
[ 33%] Building CXX object CMakeFiles/MathTest.dir/src/Driver.cpp.o
/Users/martin/Android/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/bin/arm-linux-androideabi-g++   -DMathTest_EXPORTS -D__STDC_INT64__ --sysroot=/Users/martin/Android/android-ndk-r7/platforms/android-8/arch-arm -fPIC -DANDROID -Wno-psabi -fsigned-char -mthumb -Wnon-virtual-dtor -Wno-long-long -ansi -Wundef -Wcast-align -Wchar-subscripts -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security -fexceptions -fno-check-new -fno-common -fstrict-aliasing -Wno-variadic-macros -Wextra -pedantic -g0 -O2 -fPIC -isystem /Users/martin/Android/android-ndk-r7/sources/cxx-stl/gnu-libstdc++/include -isystem /Users/martin/Android/android-ndk-r7/sources/cxx-stl/gnu-libstdc++/libs/armeabi/include -I/Users/martin/Repositories/MathTest/lib/cml-1_0_2 -I/Users/martin/Repositories/MathTest/lib/eigen-eigen-13a11181fc5a -I/Users/martin/Repositories/MathTest/lib/glm-0.9.3.B   -o CMakeFiles/MathTest.dir/src/Driver.cpp.o -c /Users/martin/Repositories/MathTest/src/Driver.cpp
"/Applications/CMake 2.8-4.app/Contents/bin/cmake" -E cmake_progress_report /Users/martin/Repositories/MathTest/build/android/CMakeFiles 2
[ 66%] Building C object CMakeFiles/MathTest.dir/src/mother.c.o
/Users/martin/Android/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/bin/arm-linux-androideabi-gcc  -DMathTest_EXPORTS -D__STDC_INT64__ --sysroot=/Users/martin/Android/android-ndk-r7/platforms/android-8/arch-arm -fPIC -DANDROID -Wno-psabi -fsigned-char -mthumb -O3 -DNDEBUG -fPIC -I/Users/martin/Android/android-ndk-r7/sources/cxx-stl/gnu-libstdc++/include -I/Users/martin/Android/android-ndk-r7/sources/cxx-stl/gnu-libstdc++/libs/armeabi/include -I/Users/martin/Repositories/MathTest/lib/cml-1_0_2 -I/Users/martin/Repositories/MathTest/lib/eigen-eigen-13a11181fc5a -I/Users/martin/Repositories/MathTest/lib/glm-0.9.3.B   -o CMakeFiles/MathTest.dir/src/mother.c.o   -c /Users/martin/Repositories/MathTest/src/mother.c
"/Applications/CMake 2.8-4.app/Contents/bin/cmake" -E cmake_progress_report /Users/martin/Repositories/MathTest/build/android/CMakeFiles 3
[100%] Building CXX object CMakeFiles/MathTest.dir/src/androidactivity.cpp.o
/Users/martin/Android/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/bin/arm-linux-androideabi-g++   -DMathTest_EXPORTS -D__STDC_INT64__ --sysroot=/Users/martin/Android/android-ndk-r7/platforms/android-8/arch-arm -fPIC -DANDROID -Wno-psabi -fsigned-char -mthumb -Wnon-virtual-dtor -Wno-long-long -ansi -Wundef -Wcast-align -Wchar-subscripts -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security -fexceptions -fno-check-new -fno-common -fstrict-aliasing -Wno-variadic-macros -Wextra -pedantic -g0 -O2 -fPIC -isystem /Users/martin/Android/android-ndk-r7/sources/cxx-stl/gnu-libstdc++/include -isystem /Users/martin/Android/android-ndk-r7/sources/cxx-stl/gnu-libstdc++/libs/armeabi/include -I/Users/martin/Repositories/MathTest/lib/cml-1_0_2 -I/Users/martin/Repositories/MathTest/lib/eigen-eigen-13a11181fc5a -I/Users/martin/Repositories/MathTest/lib/glm-0.9.3.B   -o CMakeFiles/MathTest.dir/src/androidactivity.cpp.o -c /Users/martin/Repositories/MathTest/src/androidactivity.cpp
Linking CXX shared library ../../android/libs/armeabi/libMathTest.so

readelf显示,我的库依赖于libstdc++libmlibclibdl,据我所知,除了libstdc++之外,所有这些都可以在设备上使用。

代码语言:javascript
复制
Martin-Foots-MacBook-Pro:android martin$ ~/Android/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/bin/arm-linux-androideabi-readelf -d ../../android/libs/armeabi/libMathTest.so 

Dynamic section at offset 0x14b0 contains 25 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libstdc++.so]
 0x00000001 (NEEDED)                     Shared library: [libm.so]
 0x00000001 (NEEDED)                     Shared library: [libc.so]
 0x00000001 (NEEDED)                     Shared library: [libdl.so]
 0x0000000e (SONAME)                     Library soname: [libMathTest.so]
 0x00000010 (SYMBOLIC)                   0x0
 0x00000019 (INIT_ARRAY)                 0x9498
 0x0000001b (INIT_ARRAYSZ)               12 (bytes)
 0x0000001a (FINI_ARRAY)                 0x94a4
 0x0000001c (FINI_ARRAYSZ)               12 (bytes)
 0x00000004 (HASH)                       0xd4
 0x00000005 (STRTAB)                     0x544
 0x00000006 (SYMTAB)                     0x234
 0x0000000a (STRSZ)                      1033 (bytes)
 0x0000000b (SYMENT)                     16 (bytes)
 0x00000003 (PLTGOT)                     0x9598
 0x00000002 (PLTRELSZ)                   136 (bytes)
 0x00000014 (PLTREL)                     REL
 0x00000017 (JMPREL)                     0x990
 0x00000011 (REL)                        0x950
 0x00000012 (RELSZ)                      64 (bytes)
 0x00000013 (RELENT)                     8 (bytes)
 0x00000016 (TEXTREL)                    0x0
 0x6ffffffa (RELCOUNT)                   4
 0x00000000 (NULL)                       0x0    

这些信息有用吗?有什么方法能让我知道这个未找到的符号是从哪里来的?

EN

回答 6

Stack Overflow用户

发布于 2012-12-02 17:57:38

那么,您需要在您自己的库之前加载gnustl_shared,如下所示:

代码语言:javascript
复制
static {
    System.loadLibrary("gnustl_shared");
    System.loadLibrary("myNativeLib1");
    System.loadLibrary("myNativeLib2");
    //.......
}
票数 1
EN

Stack Overflow用户

发布于 2013-02-08 19:05:14

在我的例子中,gnustl_shared库实际上不在设备上(运行Android2.3.6)。当我静态链接gnustl时,NDK应用程序在设备上运行良好。我的2美分

票数 1
EN

Stack Overflow用户

发布于 2012-01-04 08:37:22

您可能还需要将其添加到Application.mk中。您链接到的项目至少这件事

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8717986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档