“未定义的参考”使用 JNI 访问我的共享库时
我正在尝试使用 JNI 构建 Botan (加密算法库)的版本来运行一些本机 C++ Android 上的程序。
我已成功使用 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: 私有本机无效 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 应用程序.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
但我不知道这个输出中是否可以看到任何错误。 另外,我还在网上搜索了这个错误,例如这里。 不幸的是我还无法解决我的问题。
所以我的问题是:我做错了什么?
I'm trying to build a version of Botan (library for cryptographic algorithms) using JNI to run a few native C++ programmes on Android.
I've managed to create a libbotan.so without any errors using the NDK tool chain (NDK R5b).
But when I'm compiling my source file (exampleError.cpp) from my Android project (Example) I get the following errors:
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
Here is my 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
}
Here is the corresponding Java class createError.java:
private native void simpleTestCall();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
...
simpleTestCall();
}
Here you can see the 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)
And last, but not least the Application.mk:
APP_ABI := armeabi armeabi-v7a
APP_PROJECT_PATH := /home/fensta/workspace/Example
APP_STL := stlport_shared
Note: the structure of the JNI folder is as follows:
Android.mk
Application.mk
botan/botan_all.h
sources/botan_all.cpp
exampleError.cpp
I also checked the content of the libbotan.so which is as follows:
/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
But I don't know if there can be seen any error in this output.
Besides, I also searched for this error online, e.g. here.
Unfortunately I haven't been able to solve my problem yet.
So my question would be: what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LOCAL_SRC_FILES 应该包含实际文件,例如
foo.c
或bar.cpp
。您收到一条消息是因为
source
未以.c
、.cpp
等结尾。nm
命令可能会提示您:唉,没有任何函数被编译到您的库中。LOCAL_SRC_FILES should have actual files like
foo.c
orbar.cpp
.You're getting a message because
source
does not end in.c
,.cpp
, etc.The
nm
command might have tipped you off: alas, there aren't any functions being compiled into your library.