Using JsonCpp on X-Cross platform library

2019-08-05 08:42发布

问题:

I'm making a library in C++ with OpenCV and JsonCpp towards building a library for Android and iOS.

On testing my library for Android, I'm making the JNI files but when I try to load the library I'm getting

java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "_ZN4Json6WriterD2Ev" referenced by "libXYZ.so"...

and that's because I think I'm not building my Json library very well.

The library that I use is this one: https://github.com/open-source-parsers/jsoncpp

My Android.mk is:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

OPENCV_CAMERA_MODULES:=off
OPENCV_INSTALL_MODULES:=on

include $(LOCAL_PATH)/jsoncpp/Android.mk
include /Users/localmac/Desktop/AndroidDevelopment/OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk

OPENCV_LIB_TYPE:=SHARED

LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_C_INCLUDES += /Users/localmac/mylibrary/OpenCVtry/
LOCAL_C_INCLUDES += /Users/localmac/Desktop/RD/OpenCVtry/Libraries/jsoncpp-master/include

LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
LOCAL_MODULE    := libXYZ
LOCAL_SRC_FILES := androidClass.cpp main.cpp utils.cpp
LOCAL_LDLIBS     += -llog -ldl

include $(BUILD_SHARED_LIBRARY)

I have no idea of how to do this.

Thank you in advance.


EDIT it's not the NDK Compiling's fault.

Even if I compile the JsonCpp, I get the

java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "_ZN4Json6WriterD2Ev" referenced by "libXYZ.so"...

EDIT My jsoncpp/Android.mk :

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_CPP_EXTENSION := .cpp LOCAL_MODULE := libJsoncpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/jsoncpp/include

LOCAL_SRC_FILES := src/lib_json/json_reader.cpp \ src/lib_json/json_value.cpp \ src/lib_json/json_writer.cpp

include $(BUILD_SHARED_LIBRARY)

回答1:

You're not linking against Jsoncpp in your makefile. You should add the following line:

LOCAL_SHARED_LIBRARIES := libJsoncpp

before the last include $(BUILD_SHARED_LIBRARY).

You must specify module names for this variable (and its sister LOCAL_STATIC_LIBRARIES), that is, what you specified for the LOCAL_MODULE variable.

Also, that spares you from specifiying the includes in the LOCAL_C_INCLUDE variable (as the makefile will include them directly when specifying the library in the variable I mentioned at the top of my post).

EDIT: For the sake of completeness, I'll add that you can specify multiple libraries like that:

LOCAL_SHARED_LIBRARIES = libJsoncpp \
                         libOpenCV \
                         ...

and the same goes for LOCAL_STATIC_LIBRARIES.