How can I specify shared libraries to load in the Android.mk when compiling with ndk-build ?
Edit: This is my Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := lib-crypto
LOCAL_SRC_FILES := libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := tema1
LOCAL_SRC_FILES := tema1.c
LOCAL_SHARED_LIBRARIES := lib-crypto
LOCAL_C_INCLUDES := /home/aleksei/openSSL0.9.8/include
include $(BUILD_EXECUTABLE)
libcrypto.so is the library that I have built for android. I want to make a program using it. Now it gives me this error:
Install : libcrypto.so => libs/armeabi/libcrypto.so
Executable : tema1
./obj/local/armeabi/libcrypto.so: undefined reference to `dladdr'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/tema1] Error 1
PREBUILT_SHARED_LIBRARY
Points to a build script used to specify a prebuilt shared library.
Unlike BUILD_SHARED_LIBRARY and BUILD_STATIC_LIBRARY, the value
of LOCAL_SRC_FILES must be a single path to a prebuilt shared
library (e.g. foo/libfoo.so), instead of a source file.
You can reference the prebuilt library in another module using
the LOCAL_PREBUILTS variable (see docs/PREBUILTS.html for more
information).
read more from http://source-android.frandroid.com/ndk/docs/ANDROID-MK.html
Show me what you have done till yet and where you facing problem.?
I had the same problem. To fix it, I did the following:
In the same directory as your Android.mk file, create a file named Application.mk
Add the following line of code into Application.mk:
"APP_PLATFORM := android-8"
If you already have an Application.mk file, just add the code in step 2 to the existing file. Now call ndk-build and see if it links. ndk-build may be compiling with an old version of libdl which does not have dladdr(). The code in step 2 will cause ndk-build to use an updated libdl which has dladdr().
Related
I'm trying to link some .so that I generated using the NDK to a new .so I'm trying to create,
as the old .so contains definitions of functions that I want to use in the new .so.
I've tried this Android.mk :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := prog_test
LOCAL_SRC_FILES := main.c
LOCAL_MODULE_TAGS := optional
LOCAL_SHARED_LIBRARIES += mylib
include $(BUILD_SHARED_LIBRARY)
$(call import-module,<tag>) # with and without
I've also tried this method I found in stackoverflow NDK - How to use a generated .so library in another project but still no succes as I get always :
prebuilt/linux-x86/toolchain/arm-linux-androideabi-4.4.x/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lmylib.so
I really appreciate any help to solve this issue.
B.R
you have to use include $(PREBUILD_SHARED_LIBRARY) instead of include $(BUILD_SHARED_LIBRARY)
I have a c library called B and it depends on other c library called A. I am sure I can successfully compile and use A through Android NDK.
Now I am trying to compile the B library using Android NDK. I have and Android project with a jni folder. My jni folder contains the A and B folders, they have the libraries c code. The jni folder also has a prebuilt folder, and it contains the a.so file (the prebuilt A library file).
My problem is that I can not build the B. I can compile it, but I cannot link it. Please, could anyone help me pointing what would be my mistake? A and B are generic names I am using to my projects, not the real ones I am using. I will list below the Android.mk files.
The Android.mk file used to build A (this file is not visible to the Android Project I described, although it has also the A code):
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libA
LOCAL_SRC_FILES := helloworld.c A/src/fileA.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/A/src
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/A/src
include $(BUILD_SHARED_LIBRARY)
The Android.mk file in the prebuilt folder:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libA
LOCAL_SRC_FILES := prebuilt/libA.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/A/src
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libB
LOCAL_SRC_FILES := B/src/fileB.c
LOCAL_SHARED_LIBRARIES := libA
include $(BUILD_SHARED_LIBRARY)
When I execute the ndk-build command I have the final output to be something like:
B/src/fileB.c:15: error: undefined reference to
'A_function' collect2: ld returned 1 exit status make: *
[obj/local/armeabi/libB.so] Error 1
Your B library files can actually access the A library, but they just don't know how. Add the required headers (.h files from the A library) to a subfolder of JNI and specify the following :
LOCAL_C_INCLUDES := $(LOCAL_PATH)/your/sub/folder
in your B project declaration.
I still had a problem, and had to declare the A library as PREBUILT_STATIC and then add to the B library
LOCAL_WHOLE_STATIC_LIBRARIES := libA
I am trying to build a shared library using prebuilt static library, the respective Android.mk file is below.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#Include kerne headers in a proper way later on
#LOCAL_C_INCLUDES:= $(LOCAL_PATH)/../../../../kernel/include
LOCAL_MODULE := sensors.$(TARGET_BOARD_PLATFORM)
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_STATIC_LIBRARIES := LibFusion_ARM_cpp
#LOCAL_LDFLAGS := $(LOCAL_PATH)/LibFusion_ARM_cpp.a
LOCAL_SHARED_LIBRARIES := liblog libcutils
#include any shared library dependencies
LOCAL_LDFLAGS := $(LOCAL_PATH)/libimu.a
LOCAL_PRELINK_MODULE := false
LOCAL_SRC_FILES := sensors_u8500.cpp
include $(BUILD_SHARED_LIBRARY)
The files present in the folder is
Android.mk , LibFusion_ARM_cpp.a , libimu.a , MEMSAlgLib_eCompass.h , sensors_u8500.cpp
The error what I am getting while building is below,
**make: *** No rule to make target `out/target/product/u8500/obj/STATIC_LIBRARIES/LibFusion_ARM_cpp_intermediates/LibFusion_ARM_cpp.a', needed by `out/target/product/u8500/obj/SHARED_LIBRARIES/sensors.montblanc_intermediates/LINKED/sensors.montblanc.so'. Stop.**
What is the issue here?
Unable to build the shared library. I mean unable to generate libsensor.so file. facing the above mentioned error. ** make: * No rule to make target
It seems that the build system cannot find LibFusion_ARM_cpp.a. AFAIK, you have to first specify a pre-built library module using PREBUILT_STATIC_LIBRARY with LibFusion_ARM_cpp.a set as LOCAL_SRC_FILES and LOCAL_MODULE as LibFusion_ARM_cpp. This will basically copy the specified static library to the default directory the build system searches through for all of the necessary modules.
I did two things to understand the case:
A. In the above shown make file, I added the below so as to ensure that this .a file gets copied into the build.
include $(CLEAR_VARS)
LOCAL_MODULE := LibFusion_ARM
LOCAL_SRC_FILES := LibFusion_ARM_cpp.a
include $(BUILD_STATIC_LIBRARY)
When I build it using mm -n, command I see this strange statement rm -f ... of the LibFusion_ARM.a. Of course the build fails saying it's not able to locate the STATIC LIB.
mkdir -p out/target/product/u8500/obj/STATIC_LIBRARIES/LibFusion_ARM_intermediates/
rm -f out/target/product/u8500/obj/STATIC_LIBRARIES/LibFusion_ARM_intermediates/LibFusion_ARM.a
echo "target StaticLib: LibFusion_ARM (out/target/product/u8500/obj/STATIC_LIBRARIES/LibFusion_ARM_intermediates/LibFusion_ARM.a)"
mkdir -p out/target/product/u8500/obj/SHARED_LIBRARIES/sensors.montblanc_intermediates/LINKED/
.
.
Why does this deletion happens? I am not sure. This is causing the problem. Please provide suggestions.
B. I added the below lines in the make file:
include $(CLEAR_VARS)
LOCAL_MODULE := LibFusion_ARM
LOCAL_SRC_FILES := LibFusion_ARM_cpp.a
include $(PREBUILT_STATIC_LIBRARY)
I see the below error:
make: *** No rule to make target `out/target/product/u8500/obj/STATIC_LIBRARIES/LibFusion_ARM_intermediates/LibFusion_ARM.a', needed by `out/target/product/u8500/obj/SHARED_LIBRARIES/sensors.montblanc_intermediates/LINKED/sensors.montblanc.so'.
Stop.
Clearly in the second case, the file LibFusion_ARM is not getting copied (even obj/STATIC_LIBRARIES/LibFusion_ARM_intermediates directory is not created).
Needless to emphasis, if I manually copy the file LibFusion_ARM_cpp.a as LibFusion_ARM.a then the build goes through.
I have a bunch of C files with extensions .c and .h. I want to compile these files with Android NDK. When I tried with only one file, NDK worked perfectly, but when I tried including other files inside this main C files with includes, I get an error. What an I missing? This is my Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := ndksetupdemo
LOCAL_SRC_FILES := mymain_c_file.c
LOCAL_C_INCLUDES := includes
include $(BUILD_SHARED_LIBRARY)
Should I include anything more?
It appears that you only link against the log library (LOCAL_LDLIBS := -llog). If you are referencing functions that are not defined in any of the included headers and in your mymain_c_file.c, you will get the undefined reference error. You will need to find out what other libraries you need to link against and list them in LOCAL_LDLIBS.
If the functions are defined in the other .c files, you need to add them to the LOCAL_SRC_FILES variable.
I compiled Sox et al with NDK. So, I have all Android-friendly shared libs.
I made a simple test file which calls a sox function.
NDK build tells me:
undefined reference to `sox_open_read'
sox_open_read is defined in sox.h. I know it's finding sox.h because it gives me a warning about that file:
In file included from (...)/sox/sox.h:19
So maybe it wants to find sox_open_read in the actual libsox.so. Well, I've tried about a 100 different ways to tell it where the sox shared lib is e.g.
LOCAL_SHARED_LIBRARY := sox
LOCAL_LDLIBS := -L$(LOCAL_PATH_FULL)/jni/libs/libsox.so
However, It will work if I specify Sox as a static library:
#LOCAL_SHARED_LIBRARY := sox
LOCAL_STATIC_LIBRARIES := sox
LOCAL_LDLIBS := -L$(LOCAL_PATH_FULL)/jni/libs/libsox.so
It's my understanding that I don't want to staticly link to the sox lib - I want to dynamically link to it.
You should define libsox.so as a prebuilt library. Create a makefile as the following and put your prebuilt libsox.so in the same directory with this makefile. After that, you can use libsox same as you've rebuilt it. Don't forget to include this makefile into your build.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libsox
LOCAL_SRC_FILES := libsox.so
include $(PREBUILT_SHARED_LIBRARY)