0

I have created a header file "abc.h" with declaration

int abc();

Then, I created a .cpp file "abc.cpp" with definition

int abc()
{ 
  return 0;
}

Now I created a static library libabc.a from above files.

I have then created a HelloWorld Android project. Then I created a jni folder in it with subfolders "header" and "src" in it. In header folder I have put abc.h and in src folder I have put "abc.cpp". Now I have created another file "xyz.cpp" in jni folder which wants to use abc() function. But when I run ndk-build command I get this error. jni/JNIMagicCleanManager.cpp:84: error: undefined reference to function abc (something like this)

How to get definition of abc() with the static library libabc.a I have put libabc.a in same folder parallel to Android.mk. Following is my Android.mk file

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
MAGIC_CLEAN_ROOT := ..
MAGIC_CLEAN_SRC_ROOT := ../$(LOCAL_PATH)/src
MAGIC_CLEAN_SRC_FILES := xyz.cpp
MAGIC_CLEAN_C_INCLUDES := $(LOCAL_PATH)/headers/

LOCAL_STATIC_LIBRARIES := magicClean
LOCAL_MODULE := myJniLib2


LOCAL_SRC_FILES := $(MAGIC_CLEAN_SRC_FILES)
LOCAL_C_INCLUDES := $(MAGIC_CLEAN_C_INCLUDES)
LOCAL_C_INCLUDES += .
include $(BUILD_SHARED_LIBRARY)

Edit 1 : Many are asking why I have abc.cpp when I have static library, this is just for keeping code only. Please tell how to call the function from static library.

Utkarsh Srivastav
  • 3,105
  • 7
  • 34
  • 53

1 Answers1

1

Before use LOCAL_STATIC_LIBRARIES you need use PREBUILT_STATIC_LIBRARY like this example, this example uses gnu static lib, but is the same for yours.

include $(CLEAR_VARS)

LOCAL_MODULE            := gnustl_static
LOCAL_CPPFLAGS += -std=c++11
LOCAL_SRC_FILES         := ${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/thumb/libgnustl_static.a
LOCAL_EXPORT_C_INCLUDES := ${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include
LOCAL_EXPORT_C_INCLUDES += ${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/include/

include $(PREBUILT_STATIC_LIBRARY)

You should put that in your Android.mk

Alex
  • 3,301
  • 4
  • 29
  • 43