Android: How to link my own static libraries correctly? - android

I have an Android project written in C++ and have a problem in linking phase. The code is put in some static libraries which should be linked together.
I have found a lot of questions and answers on the net about this topic and most of them suggest to put my libraries to LOCAL_STATIC_LIBRARIES in the Android.mk file. But, if I do this, I found the content of LOCAL_STATIC_LIBRARIES is simply ignored: my libraries are not linked, and adding any dummy text here does not generate any error or warning message.
I tried it this way:
LOCAL_STATIC_LIBRARIES := MyLib.a
or with full path:
LOCAL_STATIC_LIBRARIES := $(LOCAL_PATH)/MyLib.a
none of them worked.
If I put my static libraries to LOCAL_LDLIBS then it is linked, but I got a warning message about non-system libraries are used, and probably the build will be wrong.
The content of my Android.mk file is:
LOCAL_LDLIBS := $(LOCAL_PATH)/MyLib.a ...
and I got this message:
Android NDK: WARNING:jni/Android.mk:myapp: non-system libraries in linker flags: jni/MyLib.a
Android NDK: This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES
Android NDK: or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the
Android NDK: current module
I could not find how to use LOCAL_STATIC_LIBRARIES right way, please help me!
I have android-ndk-r9 and android-sdk_r22.2.1 on a OpenSuSE x86 and using target=android-18

See JBL's answer here.
The LOCAL_STATIC_LIBRARIES variable does not work that way. First you need a section that defines the library you want to include:
include $(CLEAR_VARS)
LOCAL_PATH = .
LOCAL_MODULE := curl
LOCAL_EXPORT_C_INCLUDES := ../curl/include
LOCAL_SRC_FILES := ../curl/lib/libcurl.a
include $(PREBUILT_STATIC_LIBRARY)
THEN, you can include it using
include $(CLEAR_VARS)
LOCAL_MODULE = mylib
CFLAGS = ...
...
LOCAL_STATIC_LIBRARIES = curl
include $(BUILD_STATIC_LIBRARY)

Most probably the problem lies in that you are giving the extension of the library:
LOCAL_STATIC_LIBRARIES := MyLib.a
I think, it should be written as:
LOCAL_STATIC_LIBRARIES := MyLib

Related

Compiling Ceres Solver With Android and Eclipse

I am trying to use ceres solver with my android application, and there does not seem to be a lot of documentation about using them both together properly. I have followed the build instructions from the ceres solver website, as well as this helpful tutorial:
http://tech.sandyeggi.com/2013/10/using-ceres-solver-in-android-ndk.html
This has gotten me far and everything is linked properly, but when I try to compile the project I get an odd error:
/Users/Steven/Documents/ceres-solver-1.10.0/include/ceres/internal/port.h:39:35: fatal error: ceres/internal/config.h: No such file or directory
Sure enough that file does not exist. But the question is why? Is it something that is supposed to be auto-generated? or created my self?
I might be important, so here is my Android.mk:
LOCAL_PATH := $(call my-dir)
LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
include $(CLEAR_VARS)
LOCAL_MODULE := ceres
LOCAL_SRC_FILES := libceres.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += /Users/Steven/Documents/android-ndk-r10d/sources/cxx-stl/stlport/stlport
LOCAL_C_INCLUDES += /Users/Steven/Documents/eigen-eigen-36fd1ba04c12/eigen-eigen-36fd1ba04c12
LOCAL_C_INCLUDES += /Users/Steven/Documents/ceres-solver-1.10.0/include
LOCAL_C_INCLUDES += /Users/Steven/Documents/ceres-solver-1.10.0/internal/ceres/miniglog
LOCAL_MODULE := DrinkMateDeveloper
LOCAL_SRC_FILES := DrinkMateDeveloper.cpp
LOCAL_STATIC_LIBRARIES = ceres
include $(BUILD_SHARED_LIBRARY)
Questions like this are best asked on the ceres-solver mailinglist.
The config.h you are looking for exists in
ceres-solver-1.10.0/config/ceres/internal/config.h
as the documentation in that file indicates
Default (empty) configuration options for Ceres.
IMPORTANT: Most users of Ceres will not use this file, when compiling Ceres
with CMake, CMake will configure a new config.h with the currently
selected Ceres compile options and copy it into the source
directory before compilation. However, for some users of Ceres
who compile without CMake, this file ensures that Ceres will
compile, with the user either specifying manually the Ceres
compile options, or passing them directly through the compiler.
you are going to have to do your own compiler defines for the various variables that are defined in
https://ceres-solver.googlesource.com/ceres-solver/+/master/cmake/config.h.in?autodive=0%2F

NDK: using user created .so when trying to build another .so

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)

Static Linking OpenCV 2.4.3.2

I would like to statically link OpenCV(2.4.3.2) to my project. Right now my Android.mk looks like this
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include /Users/jamiematthews/Documents/Android-Projects/OpenCV-2.4.3-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := jni_part
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS += -llog -ldl
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
Which throws the errors:
Android NDK: ERROR:/Users/jamiematthews/Documents/workspace/AuthentiGuard/jni/Android.mk:on: LOCAL_SRC_FILES points to a missing file
/Users/jamiematthews/Documents/android-ndk-r8d/build/core/prebuilt-library.mk:43: *** Android NDK: Aborting . Stop.
Android NDK: Check that /Users/jamiematthews/Documents/Android-Projects/OpenCV-2.4.3-android-sdk/sdk/native/jni/../libs/mips/libon.so exists or that its path is correct
Anyone know what "LOCAL_SRC_FILES points to a missing file" could mean?
PS if I remove the lines
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
it compiles fine, but then I cant statically link
Found the solution. Believe it or not it was an issue with Application.mk, not Android.mk. I had set
APP_ABI := all
Previously, and not even considered that it could effect the build process of this statically. I ended up changing it to
APP_ABI := armeabi-v7a
And now, everything works fine. Note that I ended up not needing OPENCV_CAMERA_MODULES:=on, I believe that is only if you are calling the camera from JNI, which I am not
LOCAL_SRC_FILES show the file where your native code resides. If you use native OpenCV code then you have to add the name of your cpp file here.
If you do not have a native part then you should add the OpenCV library to the libs folder of your project. (in case of static initialization). Please check this page for details.

Build issue when using prebuilt libraries with the Android NDK

I have a program I am porting that links together multiple libraries when creating the executable. I have built all those libraries using the stand alone toolchain and using the standalone toolchain I am able to create an executable that works on an android device. So, it seems like the libraries I have built are functional. Now I am trying to incorporate those libraries with an app. So, in my android.mk I have something like this:
LOCAL_PATH := $(call my-dir)
ROOT_PATH := $(LOCAL_PATH)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
LOCAL_PATH = $(ROOT_PATH)
LOCAL_MODULE := test-libs
LOCAL_STATIC_LIBRARIES := staticA
LOCAL_SHARED_LIBRARIES := sharedA sharedB sharedC sharedD
LOCAL_SRC_FILES := test-libs.c
include $(BUILD_SHARED_LIBRARY)
For each of the libraries, I have a Android.mk like this
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sharedA
LOCAL_SRC_FILES := sharedA.so
include $(PREBUILT_SHARED_LIBRARY)
When I then build my project (in eclipse), I get this:
C:/ndk/android-ndk-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a(unwind-arm.o): In function `__gnu_unwind_pr_common':
/cygdrive/c/ndk/android-ndk-r7b/build/core/build-binary.mk:314: recipe for target `obj/local/armeabi/libtest-libs.so' failed
/tmp/ndk-digit/src/build/../gcc/gcc-4.4.3/libgcc/../gcc/config/arm/unwind-arm.c:1237: undefined reference to `__cxa_call_unexpected'
Any thoughts on what is going wrong?
Also, the static library and one of the shared libraries have no dependencies on anything and if I only include them all is cool. One of my shared libraries only had a dependency on the static library. If I only include those, but when I include the others, which have dependencies on other shared libraries, this problem occurs.
Update 1: Ok it appears to be because the APP_STL setting in my Application.mk was being ignored. All I have in my Application.mk is:
APP_STL := gnustl_shared
If I copy over the libgnustl_shared.so and treat it like another prebuilt shared lib, my problem is gone. Any idea why the APP_STL is not working properly. Note, I could have screwed something up. I just upgraded to using 7b. Using gnustl_shared used to work for me with other apps. Rolling back to 7 doesn't fix it. I think I have messed something up in Eclipse. I use Eclipse (windows) with sequoyah.
It looks like the linker is giving you an error. What you should do is the following:
Add a LOCAL_LDLIBS under your LOCAL_MODULE := test-libs. Here you need to include all the libraries you link against when you compile your pre-compiled libraries. So for example:
LOCAL_LDLIBS := -lgnustl_shared -lgcc -llog -landroid -lstdc++
Basically you need to identify what library contains the function __cxa_call_unexpected. A quick google shows that it's probably in libstdc++. Make sure that you also link with this library when creating your pre-compiled libraries.
I'm thinking it might have something to do with exceptions support.
Are you using exceptions in your code and if so are you compiling with a runtime library that supports exceptions? (and compiling with exceptions on)?.
There is more on this in the CPLUSPLUS-SUPPORT and STANDALONE-TOOLCHAIN files in the ndk docs.
I've observed a similar problem when one of my projects which contains only C source files (*.c) references another project that contains a c++ file(*.cpp). Application.mk files for both projects had APP_STL := gnustl_shared in them. The ndk version is ndk7e.
The solution was adding an empty C++ file (dummy.cpp) to the project that contained only .c files. Supposedly ndk understood that this project should be linked against the gnustl_shared and the build succeeded.

Android NDK linking problems

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)

Categories

Resources