Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build an shared library and call it in other ndk program

I want to build an shared library. To build it, I need to call another shared library. Here is what I did:

1.Create one Android project,named "BuildLib",and add a new folder "jni" under the project directory. Contents of jni folder:

jni-->Android.mk
-->Application.mk
-->add.cpp
-->add.h add.cpp just do two numbers addition:

add.h:

int add(int a,int b);

add.cpp:

#include "add.h"  
int add(int a,int b){
    return a+b;}

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES  := add.cpp 
LOCAL_MODULE     := add
include $(BUILD_SHARED_LIBRARY)

After build the project,I got libadd.so under directory $(BUILDLIB)/libs/armeabi/.

Create another Android project, named "CallLib". Copy libadd.so and add.h to jni folder, create Android.mk, Application.mk, and call_add.cpp.

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := libadd.so
LOCAL_MODULE := add_prebuilt
include $(PREBUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES  := call_add.cpp 
LOCAL_MODULE     :=  native
LOCAL_SHARED_LIBRARIES := add_prebuilt
include $(BUILD_SHARED_LIBRARY)

call_add.cpp:

#include "add.h"
int call_add(){return add(1,2);}

After all above, I build the CallLib project, but got the error:

undefined reference to 'add(int, int)';

I think the libadd.so can not be found, but I don't know how to modify. Does anyone know how I can fix this? Any help will be appreciated.

like image 638
user2591946 Avatar asked Jul 18 '13 04:07

user2591946


People also ask

How do I use prebuilt .so lib files?

Declare a prebuilt library Give the module a name. This name does not need to be the same as that of the prebuilt library, itself. In the module's Android.mk file, assign to LOCAL_SRC_FILES the path to the prebuilt library you are providing. Specify the path relative to the value of your LOCAL_PATH variable.

What is .MK file in Android?

Overview. The Android.mk file resides in a subdirectory of your project's jni/ directory, and describes your sources and shared libraries to the build system. It is really a tiny GNU makefile fragment that the build system parses once or more.

How do shared object libraries work?

Simply put, A shared library/ Dynamic Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn't require the code to be copied, it is done by just placing name of the library in the binary file.

What is NDK Sysroot?

$NDK/sysroot is the unified headers and is what you should be using when compiling. When linking, you should still be using $NDK/platforms/android-$API/arch-$ARCH . We do have a doc explaining how to use unified headers in a custom build system: android.googlesource.com/platform/ndk/+/master/docs/….


1 Answers

In your second Android.mk, try replacing the first module with:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := libadd.so
LOCAL_MODULE := add_prebuilt
LOCAL_EXPORT_C_INCLUDES := add.h
include $(PREBUILD_SHARED_LIBRARY)

The LOCAL_EXPORT_C_INCLUDES flag should attach the header information to the add_prebuilt module, so it can be linked with your final library.

like image 90
mbrenon Avatar answered Oct 20 '22 06:10

mbrenon