使用JNI访问我的共享库时出现“未定义的引用”

时间:2011-03-05 18:31:11

标签: android java-native-interface undefined-reference botan

我正在尝试使用JNI在Android上运行一些原生C ++程序来构建Botan版本(加密算法库)。 我设法使用NDK工具链(NDK R5b)创建了一个没有任何错误的libbotan.so。 但是当我从我的Android项目(示例)编译我的源文件(exampleError.cpp)时,我收到以下错误:

Android NDK: WARNING: Unsupported source file extensions in /home/fensta/workspace 
/Example/jni/Android.mk for module botan    
Android NDK:   sources    
Android NDK: WARNING: Unsupported source file extensions in /home/fensta/workspace  
/Example/jni/Android.mk for module botan    
Android NDK:   sources    
Install        : libbotan.so => libs/armeabi/libbotan.so
Compile++ thumb  : fooBar <= exampleError.cpp
SharedLibrary  : libfooBar.so
/home/fensta/workspace/Example/obj/local/armeabi/objs/fooBar/exampleError.o: In  
function `LibraryInitializer':
/home/fensta/workspace/Example/jni/botan/botan_all.h:5593: undefined reference to `  
Botan::LibraryInitializer::initialize(std::basic_string<char, std::char_traits<char>,   
std::allocator<char> > const&)'
/home/fensta/workspace/Example/obj/local/armeabi/objs/fooBar/exampleError.o: In  
function `~LibraryInitializer':
/home/fensta/workspace/Example/jni/botan/botan_all.h:5595: undefined reference to `  
Botan::LibraryInitializer::deinitialize()'
/home/fensta/workspace/Example/jni/botan/botan_all.h:5595: undefined reference to `
Botan::LibraryInitializer::deinitialize()'
/home/fensta/workspace/Example/obj/local/armeabi/objs/fooBar/exampleError.o: In 
function `~LibraryInitializer':
/home/fensta/Programs/android-ndk-r5b/sources/cxx-stl/stlport/stlport   
/stl/_string_base.h:156: undefined reference to `
Botan::LibraryInitializer::deinitialize()'
collect2: ld returned 1 exit status
make: *** [/home/fensta/workspace/Example/obj/local/armeabi/libfooBar.so] Error 1

这是我的exampletError.cpp:

#include <jni.h>
#include <string>
#include <botan/botan_all.h>
using namespace Botan;

JNIEXPORT void JNICALL Java_test_example_example_simpleTestCall (JNIEnv *env, jobject  
object){
    LibraryInitializer init;// <- calling a random type from Botan fails
}

这是相应的Java类createError.java:     private native void simpleTestCall();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    simpleTestCall();
}

在这里你可以看到Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := libbotan
LOCAL_CPPFLAGS += -fexceptions
LOCAL_SRC_FILES :=  sources 
LOCAL_C_INCLUDES := includes 
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := fooBar
LOCAL_SRC_FILES := exampleError.cpp
LOCAL_SHARED_LIBRARIES:=  
libbotan                                      
LOCAL_CPPFLAGS += -fexceptions
include $(BUILD_SHARED_LIBRARY)

最后,但并非最不重要的是Application.mk:

APP_ABI := armeabi armeabi-v7a
APP_PROJECT_PATH := /home/fensta/workspace/Example
APP_STL := stlport_shared

注意:JNI文件夹的结构如下: Android.mk Application.mk 牡丹/ botan_all.h 来源/ botan_all.cpp exampleError.cpp

我还检查了libbotan.so的内容,如下所示:

/workspace/Example/obj/local/armeabi$ nm libbotan.so 
00001234 a _DYNAMIC
000012bc a _GLOBAL_OFFSET_TABLE_
000012c8 A __bss_end__
000012c8 A __bss_start
000012c8 A __bss_start__
000012c8 D __data_start
000012c8 A __end__
00000233 A __exidx_end
00000233 A __exidx_start
000012c8 A _bss_end__
000012c8 A _edata
000012c8 A _end

但是我不知道在这个输出中是否可以看到任何错误。 此外,我还在网上搜索了这个错误,例如here。 不幸的是,我还没能解决我的问题。

所以我的问题是:我做错了什么?

1 个答案:

答案 0 :(得分:3)

LOCAL_SRC_FILES应包含foo.cbar.cpp等实际文件。

您收到的消息是因为source未在.c.cpp等中结束。

nm命令可能会让你失望:唉,你的库里没有编译任何函数。