0

I have been trying to compile my shared library which includes OpenCV libraries. My sample android make files is as below:

LOCAL_PATH := (call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libopencvXXX
LOCAL_SRC_FILES := libs/opencv/libopencvXXX.a
include $(PREBUILT_STATIC_LIBRARY)

OPENCV_LIB_TYPE := STATIC
OPENCV_INSTALL_MODULES := on
OPENCV_CAMERA_MODULES := on

LOCAL_MODULE := libXXX
LOCAL_CPPFLAGS := -std=c++11 -fexceptions
LOCAL_CPP_FEATURES += exceptions
LOCAL_CFLAGS := -std=gnu++11 -fexceptions
LOCAL_LDLIBS := -llog -ldl

LOCAL_SRC_FILES := XXX.cpp
LOCAL_C_INCLUDES := <path to opencv header files>

LOCAL_STATIC_LIBRARIES := stdc++
LOCAL_STATIC_LIBRARIES += libopencvXXX

include $(BUILD_SHARED_LIBRARY)

However, it keeps throwing below errors:

[INFO] jni/libs/opencv/libopencv_core.a(persistence.cpp.o):persistence.cpp:function icvGets(CvFileStorage*, char*, int): error: undefined reference to 'gzgets'
[INFO] jni/libs/opencv/libopencv_core.a(persistence.cpp.o):persistence.cpp:function icvXMLSkipSpaces(CvFileStorage*, char*, int): error: undefined reference to 'gzeof'

I tried several options like below.

LOCAL_LDLIBS += -lz
LOCAL_LDLIBS += -L<NDK root>/platforms/android-19/arch-arm/usr/lib -lz

But nothing helped. Does someone has any clue how I can fix this?

EDIT: Forgot to add that when I add these above two options including '-lz', it throws an error saying "No native compiled library found, did the native compile complete successfully!".

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
pree
  • 2,297
  • 6
  • 37
  • 55
  • I hope that **--log** instead of **-llog** is a copy/paste mistake, not a typo in your **Android.mk**. And you definitely don't need `-fexceptions` for **LOCAL_CFLAGS** – Alex Cohn Mar 20 '14 at 22:45
  • Yes, it was a typo. I fixed it. – pree Mar 20 '14 at 22:46
  • I hope you are working with ndk r9d, not r5 as the tag suggests – Alex Cohn Mar 20 '14 at 22:52
  • Ah. I should have mentioned that. I'm using ndk-r9c. – pree Mar 20 '14 at 22:53
  • if you run `ndk-build V=1`, you will see the full link command in your console. Does it have `-lz`? – Alex Cohn Mar 20 '14 at 22:54
  • I'm running it all using maven. However, I tried running it in verbose mode and I didn't get any more info. However, I noticed that it displays SharedLibrary : liblibtiff.so (It's 3rdparty library included in openCV SDK and I statically linked to it as well, using .A version of it). I'm pretty new to this stuff, sorry if I missed something. – pree Mar 20 '14 at 23:03
  • maven-shmaven, when it comes to the native build, you can simply open terminal, cd to the project root (so that you can `ls -l jni/Android.mk`), and run `ndk-build V=1`. – Alex Cohn Mar 20 '14 at 23:09
  • why not add `-lz` to the same line with `-llog -ldl`? – Alex Cohn Mar 20 '14 at 23:11
  • 1
    Oh yes, of course I tried that too but gave the same error mentioned in my edited question. Thanks for the above native build commands, I'm able to run them and I again notice that it's trying to get to some liblibtiff.so which I'm actually statically linking. The build stops right after that. – pree Mar 20 '14 at 23:39

1 Answers1

0

I found the issue. I was supposed to add

include $(CLEAR_VARS)

after including all static libraries. Below is the corrected Android.mk

LOCAL_PATH := (call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libopencvXXX
LOCAL_SRC_FILES := libs/opencv/libopencvXXX.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)

OPENCV_LIB_TYPE := STATIC
OPENCV_INSTALL_MODULES := on
OPENCV_CAMERA_MODULES := on

LOCAL_MODULE := libXXX
LOCAL_CPPFLAGS := -std=c++11 -fexceptions
LOCAL_CPP_FEATURES += exceptions
LOCAL_CFLAGS := -std=gnu++11 -fexceptions
LOCAL_LDLIBS := -llog -ldl

LOCAL_SRC_FILES := XXX.cpp
LOCAL_C_INCLUDES := <path to opencv header files>

LOCAL_STATIC_LIBRARIES := stdc++
LOCAL_STATIC_LIBRARIES += libopencvXXX

include $(BUILD_SHARED_LIBRARY)
pree
  • 2,297
  • 6
  • 37
  • 55