Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK - problem linking an external library (can't found it)

I am working with Android NDK r6b under cygwin (the system is updated correctly). I am modifying the hello-jni sample in order to learn working with NDK. Since i have a library written in C++ that i wish to use in the hello-jni (actually, i have created a prj called helloworld with a single .cpp file called ndkfoo.cpp) sample, i created a new Android project in Eclipse (updated for Android), added a jni directory, added a Android.mk and Application.mk files, edited them in order to compile the .cpp. At the end of the compilation, i obtain a .so file.

Now, in the helloworld Android.mk, i need to make some edits in order to tell the linker to include that library. Suppose the library file is libmylib.so, i have the following android.mk script:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := ndkfoo

LOCAL_SRC_FILES := ndkfoo.cpp

LOCAL_C_INCLUDES += $(LOCAL_PATH)/mylib

LOCAL_LDLIBS += -L$(LOCAL_PATH)/../../mylib/libs/armeabi/ -lmylib

include $(BUILD_SHARED_LIBRARY)

My directories are organized in the following way:

d:/android/android-ndk-r6b => android ndk root
d:/android/workspace/helloworld => helloworld project
d:/android/workspace/mylib => mylib project library

(therefore, the path to libmylib.so is: d:/android/workspace/mylib/libs/armeabi).

Unfortunately, this doesn't seems to work. If i remove every reference to mylib from ndkfoo.cpp, it compiles and run even on my phone. If i do not remove references to mylib, it compiles but doens't link: i obtain the following result:

D:/android/android-ndk-r6b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windo ws/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/ bin/ld.exe: cannot find -lmylib

Ps. I forgot to mention that i run ndk-buld under the jni directory of the helloworld project. Pss. I have found a lot of similar questions over the internet. I have always worked with Visual C/C++ IDE, so i am really new to GCC, makefiles and so on...

like image 488
Luke Avatar asked Nov 13 '22 17:11

Luke


1 Answers

The message

D:/android/android-ndk-r6b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windo ws/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/ bin/ld.exe: cannot find -lmylib

is indicating you that the linker is not finding your library, this should come from a problem in the LD_LIBS' path.

I think that my-dir macro does not include devices' unit identifier so, your LOCAL_PATH variable should miss the D: and, I think, won't work with cygwin. I'm a Linux user and I'm not 100% sure but, if you replace

LOCAL_PATH := $(call my-dir)

by

LOCAL_PATH := D:$(call my-dir)

it should work. On the other hand you can always set the relative path by setting:

LOCAL_LDLIBS += -L$../../mylib/libs/armeabi/ -lmylib
like image 73
jcm Avatar answered Dec 25 '22 21:12

jcm