JonasVautherin
JonasVautherin

Reputation: 8063

Include prebuilt static library

I created a static C++ library for testing. It only defines a class, MyLibrary, and the constructor MyLibrary::MyLibrary(). I built it in qtcreator, and got a libMyLibrary.a file, which is a prebuilt static library.

I would like to use this library in an Android project (using the NDK). In a working NDK test project, I therefore added a folder called inc at the same level as jni, in which I put libMyLibrary.a and its corresponding header mylibrary.h.

My Android.mk is as follows:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := MyLibrary
LOCAL_SRC_FILES := ../inc/libMyLibrary.a

include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := helloJNI
LOCAL_SRC_FILES := mainActivity.cpp

LOCAL_C_INCLUDES += inc
LOCAL_STATIC_LIBRARIES := MyLibrary

LOCAL_LDLIBS    := -llog
include $(BUILD_SHARED_LIBRARY)

The ndk-build command compiles without any error. But my static library is apparently not linked (i.e. it does not appear in obj/local/armeabi/objs).

I have tried to include it in my mainActivity.cpp file, but even though the header (mylibrary.h) is found, the library is not and I cannot create an object as in:

MyLibrary test = MyLibrary();

What am I doing wrong? I have read tens of similar questions on StackOverflow, but I still don't get it.

Upvotes: 5

Views: 15921

Answers (1)

yushulx
yushulx

Reputation: 12160

try to use this:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := helloJNI
LOCAL_SRC_FILES := mainActivity.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc/
LOCAL_LDLIBS    := -llog -L$(LOCAL_PATH)/inc/ -lMyLibrary
include $(BUILD_SHARED_LIBRARY)

move libMyLibrary.a & mylibrary.h to jni/inc/libMyLibrary.a

Upvotes: 5

Related Questions