Android NDK下链接共享库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10874348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Link shared library under Android NDK
提问by Hyman
I with success compile library LibXtract to shared object libxtract.so and want to use is in second project.
我成功地将库 LibXtract 编译为共享对象 libxtract.so 并希望在第二个项目中使用。
In mention project I try to compile it on simple function:
在提到的项目中,我尝试在简单的函数上编译它:
#include <com_androidnative1_NativeClass.h>
#include <android/log.h>
#include "libxtract.h"
JNIEXPORT void JNICALL Java_com_androidnative1_NativeClass_showText
(JNIEnv *env, jclass clazz)
{
float mean = 0, vector[] = {.1, .2, .3, .4, -.5, -.4, -.3, -.2, -.1}, spectrum[10];
int n, N = 9;
float argf[4];
argf[0] = 8000.f;
argf[1] = XTRACT_MAGNITUDE_SPECTRUM;
argf[2] = 0.f;
argf[3] = 0.f;
xtract[XTRACT_MEAN]((void *)&vector, N, 0, (void *)&mean);
__android_log_print(ANDROID_LOG_DEBUG, "LIbXtract", "Button pushe2");
}
I have flat structure:
我有扁平结构:
- jni/com_androidnative1_NativeClass.c
- jni/com_androidnative1_NativeClass.hjni/libxtract.h
- jni/other *.h files from libxtract interface
- jni/Android.mk
- jni/Applicatoin.mk
- jni/com_androidnative1_NativeClass.c
- jni/com_androidnative1_NativeClass.hjni/libxtract.h
- 来自 libxtract 接口的 jni/other *.h 文件
- jni/Android.mk
- jni/Applicatoin.mk
library libxtract.so I put in mainproject/lib folder
库 libxtract.so 我放在 mainproject/lib 文件夹中
my Android.mk file looks like:
我的 Android.mk 文件看起来像:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := com_androidnative1_NativeClass.c
LOCAL_MODULE := com_androidnative1_NativeClass
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/
LOCAL_LDLIBS += -llog
LOCAL_SHARE_LIBRARIES := libxtract
NDK_MODULE_PATH += $(LOCAL_PATH)/../lib/
include $(BUILD_SHARED_LIBRARY)
and I still got error:
我仍然有错误:
Compile thumb : com_androidnative1_NativeClass <= com_androidnative1_NativeClass.c
SharedLibrary : libcom_androidnative1_NativeClass.so./obj/local/armeabi/objs/com_androidnative1_NativeClass/com_androidnative1_Nativ eClass.o: In function `Java_com_androidnative1_NativeClass_showText':
/home/Hyman/Projects/AndroidNative1/jni/com_androidnative1_NativeClass.c:20: undefined reference to `xtract'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libcom_androidnative1_NativeClass.so] Error 1
Code came form example of LibXtract and under C++ compile without problems, any ideas?
代码来自 LibXtract 的示例,在 C++ 下编译没有问题,有什么想法吗?
采纳答案by Suvam Roy
Your Android make file should be ...
您的 Android 制作文件应该是...
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LIB_PATH := $(LOCAL_PATH)/../lib
LOCAL_SRC_FILES := com_androidnative1_NativeClass.c
LOCAL_MODULE := com_androidnative1_NativeClass
LOCAL_LDLIBS += -llog
LOCAL_LDLIBS += $(LIB_PATH) -lxtract
LOCAL_LDLIBS += $(LIB_PATH) -lxtract
LOCAL_SHARE_LIBRARIES := libxtract
include $(BUILD_SHARED_LIBRARY)
Try this make file in your second project, and you can successfully build your code without having any error.
在你的第二个项目中试试这个 make 文件,你可以成功地构建你的代码而不会出现任何错误。
回答by Sattar Hummatli
In the above answer all is right but exept one.
在上面的答案中,一切都是正确的,但只有一个。
When we want to link lib we must add -L
before LOCAL_LDLIBS
dir as below.
当我们想要链接 lib 时,我们必须add -L
在LOCAL_LDLIBS
dir之前,如下所示。
LIB_PATH := $(LOCAL_PATH)/../lib
LOCAL_LDLIBS += **-L**$(LIB_PATH) -lxtract
Else it will give error as below
否则它会给出如下错误
cannot open XXX/../lib: Permission denied
无法打开 XXX/../lib: 权限被拒绝
回答by FooF
You need to tell Android NDK build scripts about your shared library. Check ${NDK}/doc/PREBUILTS.html
for instructions how this can be done. They advise to add Android.mk
in the same directory where you have your libXtract.so
:
您需要告诉 Android NDK 构建脚本有关您的共享库的信息。检查${NDK}/doc/PREBUILTS.html
如何做到这一点的说明。他们建议Android.mk
在您拥有以下内容的同一目录中添加libXtract.so
:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libXtract
LOCAL_SRC_FILES := libXtract.so
include $(PREBUILT_SHARED_LIBRARY)
Debugging tip: I guess you are using ndk-build
to build your "second project". Try running ndk-build
with V=99
(try V=99 ndk-build
or ndk-build V=99
- my memory failing). This will show you the the exact failing linking command. You should likely have options -lXtract
and -L/path/to/libXtract/library
. (Sometimes it is convenient to just copy and paste the linking command to run it manually to find the right options for successful linking, before actually fixing the build settings.)
调试提示:我猜您正在使用ndk-build
构建您的“第二个项目”。尝试运行ndk-build
用V=99
(尝试V=99 ndk-build
或ndk-build V=99
-我的记忆中不及格)。这将向您显示确切的失败链接命令。您可能应该有选项-lXtract
和-L/path/to/libXtract/library
. (有时,在实际修复构建设置之前,只需复制并粘贴链接命令以手动运行它以找到成功链接的正确选项会很方便。)
Update:I now see @codetiger's comment seems to point to a same sort of answer (without mentioning the NDK document which is good reading - so I am not deleting this answer).
更新:我现在看到@codetiger 的评论似乎指向了同样的答案(没有提到很好阅读的 NDK 文档——所以我不会删除这个答案)。