Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to link my own static libraries correctly?

Tags:

c++

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

like image 897
user2849906 Avatar asked Oct 05 '13 17:10

user2849906


People also ask

How are static libraries linked?

Static libraries are created by copying all necessary library modules used in a program into the final executable image. The linker links static libraries as a last step in the compilation process. An executable is created by resolving external references, combining the library routines with program code.

What happens when you link a static library?

Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system. This option forces the linker to place the library procedures your program references into the program's object file.

What is the difference between a static and shared library?

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

Can a static library have dependencies?

So what is a Static library?? When linked like this the library is called a static library, because the library will remain unchanged unless the program is recompiled. This is the most straight forward way of using a library as the final result is a simple executable with no dependencies.


1 Answers

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)
like image 120
Colin Avatar answered Sep 21 '22 18:09

Colin