Link a shared library under Android NDK

I have successfully compiled the LibXtract library for the generic libxtract.so object and want to use it in the second project.

In the mention project, I'm trying to compile it into a 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 a flat structure:

  • JNI / com_androidnative1_NativeClass.c
  • JNI / com_androidnative1_NativeClass.hjni / libxtract.h
  • jni / other * .h files from the libxtract interface
  • JNI / Android.mk
  • JNI / Applicatoin.mk

libxtract.so library I put in the mainproject / lib folder

my Android.mk file looks like this:

 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 the 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/jack/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 

The code appeared in the form of a LibXtract example and under C ++ it compiles without problems, any ideas?

+6
source share
3 answers

Your Android file must be ...

 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) -page

 LOCAL_SHARE_LIBRARIES := libxtract include $(BUILD_SHARED_LIBRARY) 

Try to make this file in the second project, and you can successfully create your code without any errors.

+4
source

You need to tell the Android NDK build scripts about your shared library. Check ${NDK}/doc/PREBUILTS.html how to do this. They advise adding Android.mk to the same directory where you have libXtract.so :

 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libXtract LOCAL_SRC_FILES := libXtract.so include $(PREBUILT_SHARED_LIBRARY) 

Debugging: I suppose you use ndk-build to create your "second project". Try running ndk-build using V=99 (try V=99 ndk-build or ndk-build V=99 - a failure in my memory). This will show you the exact command of the failed link. You should probably have the -lXtract and -L/path/to/libXtract/library . (Sometimes itโ€™s convenient to just copy and paste the build command to run it manually to find the options you need for a successful build before actually committing the build settings.)

Update: Now I see a comment by @codetiger seems to point to the same answer (without mentioning the NDK document, which is a good read, so I am not deleting this answer).

+2
source

In the answer above, everything is correct, but with the exception of one.

If we want to bind lib, we must add -L before the LOCAL_LDLIBS dir, as shown below.

 LIB_PATH := $(LOCAL_PATH)/../lib LOCAL_LDLIBS += **-L**$(LIB_PATH) -lxtract 

Otherwise, it will give an error as shown below.

cannot open XXX /../ lib: Permission denied

+2
source

Source: https://habr.com/ru/post/917319/


All Articles