Android JNI:无法链接到静态库(未定义的引用)

Android JNI: Fail to link to static library (Undefined Reference)

提问人:WaHaHa_ZegaPain 提问时间:8/5/2014 最后编辑:WaHaHa_ZegaPain 更新时间:8/6/2014 访问量:1026

问:

我是 Android JNI 的新手,我需要在我的项目中使用静态库 (librwl.a)。但是我现在有错误消息未定义引用。任何人都可以善意地建议问题是什么?提前致谢

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := staticlibrwl
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := prebuild/librwl.a

include $(PREBUILT_STATIC_LIBRARY)


include $(CLEAR_VARS)
LOCAL_MODULE := jrwl
LOCAL_SRC_FILES := rwl.cpp

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_WHOLE_STATIC_LIBRARIES := staticlibrwl

include $(BUILD_SHARED_LIBRARY)

rwl.cpp

#include <jni.h>

#include "include/librwl.h"

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jint JNICALL Java_ms_android_pos_NativeRWL_Format
  (JNIEnv * env, jobject obj){
    return Format();
}

JNIEXPORT jint JNICALL Java_ms_android_pos_NativeRWL_InitActionList
  (JNIEnv * env, jobject obj){
    return InitActionList();
}

#ifdef __cplusplus
}
#endif

页眉:

#ifdef __cplusplus
extern "C" {
#endif

extern int Format();
extern int InitActionList();

#ifdef __cplusplus
}
#endif

错误信息:

D:\Project\android-ndk-r10\ndk-build.cmd all 
Android NDK: WARNING: APP_PLATFORM android-17 is larger than android:minSdkVersion 10 in ./AndroidManifest.xml    
[armeabi] Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
[armeabi] Gdbsetup       : libs/armeabi/gdb.setup
[armeabi] Compile++ thumb: jrwl <= rwl.cpp
[armeabi] SharedLibrary  : libjrwl.so
D:/Project/android-ndk-r10/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs-debug/jrwl/rwl.o: in function Java_ms_android_pos_NativeRWL_Format:jni/rwl.cpp:12: error: undefined reference to 'Format'
D:/Project/android-ndk-r10/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs-debug/jrwl/rwl.o: in function Java_ms_android_pos_NativeRWL_InitActionList:jni/rwl.cpp:18: error: undefined reference to 'InitActionList'
collect2: ld returned 1 exit status
make.exe: *** [obj/local/armeabi/libjrwl.so] Error 1

使用 ndk-build V=1 -B 后出现错误消息(在 2014 年 8 月 6 日编辑):

D:\Project\android-ndk-r10\ndk-build.cmd V=1 -B all 
Android NDK: WARNING: APP_PLATFORM android-17 is larger than android:minSdkVersion 10 in ./AndroidManifest.xml    
del /f/q .\libs\armeabi\gdbserver     >NUL 2>NUL
del /f/q .\libs\armeabi\gdb.setup     >NUL 2>NUL
[armeabi] Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
copy /b/y "D:\Project\android-ndk-r10\prebuilt\android-arm\gdbserver\gdbserver" ".\libs\armeabi\gdbserver" > NUL
[armeabi] Gdbsetup       : libs/armeabi/gdb.setup
D:/Project/android-ndk-r10/prebuilt/windows-x86_64/bin/echo.exe "set solib-search-path ./obj/local/armeabi" > ./libs/armeabi/gdb.setup
D:/Project/android-ndk-r10/prebuilt/windows-x86_64/bin/echo.exe "directory D:/Project/android-ndk-r10/platforms/android-17/arch-arm/usr/include jni/include D:/Project/android-ndk-r10/sources/cxx-stl/system/include" >> ./libs/armeabi/gdb.setup
[armeabi] Compile++ thumb: jrwl <= rwl.cpp
D:/Project/android-ndk-r10/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ -MMD -MP -MF ./obj/local/armeabi/objs-debug/jrwl/rwl.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-exceptions -fno-rtti -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -O0 -UNDEBUG -marm -fno-omit-frame-pointer -Ijni/include -ID:/Project/android-ndk-r10/sources/cxx-stl/system/include -Ijni/include -Ijni -DANDROID  -Wa,--noexecstack -Wformat -Werror=format-security      -ID:/Project/android-ndk-r10/platforms/android-17/arch-arm/usr/include -c  jni/rwl.cpp -o ./obj/local/armeabi/objs-debug/jrwl/rwl.o 
[armeabi] StaticLibrary  : libstdc++.a
del /f/q obj\local\armeabi\libstdc++.a >NUL 2>NUL
D:/Project/android-ndk-r10/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/arm-linux-androideabi-ar crsD ./obj/local/armeabi/libstdc++.a 
[armeabi] SharedLibrary  : libjrwl.so
D:/Project/android-ndk-r10/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ -Wl,-soname,libjrwl.so -shared --sysroot=D:/Project/android-ndk-r10/platforms/android-17/arch-arm ./obj/local/armeabi/objs-debug/jrwl/rwl.o -Wl,--whole-archive jni/prebuild/librwl.a -Wl,--no-whole-archive ./obj/local/armeabi/libstdc++.a -lgcc -no-canonical-prefixes  -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now  -LD:/Project/android-ndk-r10/platforms/android-17/arch-arm/usr/lib -L./prebuild -lstdc++ -lc -lm -o ./obj/local/armeabi/libjrwl.so
D:/Project/android-ndk-r10/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs-debug/jrwl/rwl.o: in function Java_ms_android_pos_NativeRWL_Format:jni/rwl.cpp:12: error: undefined reference to 'Format'
D:/Project/android-ndk-r10/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs-debug/jrwl/rwl.o: in function Java_ms_android_pos_NativeRWL_InitActionList:jni/rwl.cpp:18: error: undefined reference to 'InitActionList'
collect2: ld returned 1 exit status
make.exe: *** [obj/local/armeabi/libjrwl.so] Error 1
Android Eclipse java-native-interface static-libraries undefined-reference

评论

0赞 Alex Barker 8/6/2014
你能提供更详细的输出吗?特别是链接器是如何调用的?我怀疑LOCAL_SRC_FILES不是静态库应该去的地方......它更像是一个对象文件,实际上它应该通过 -L./prebuild/ -lrwl 传递给链接器。
0赞 WaHaHa_ZegaPain 8/6/2014
@Alex感谢您的评论。我在互联网上搜索并遵循了一些示例(tariqzubairy.wordpress.com/2012/03/12/...),这就是为什么我将静态库放在LOCAL_SRC_FILES,如我的代码所示。您能否详细说明如何使用 -L./prebuild/ -lrwl 将静态库传递给链接器?我不确定如何在eclipse中做到这一点。多谢
0赞 WaHaHa_ZegaPain 8/6/2014
@Alex我尝试运行“ndk-build V=-1 -B”并编辑了显示的错误消息。是否有任何提示显示我的问题是什么?谢谢

答: 暂无答案