Android NDK build shared library - android

I am new in NDK and i want to use a c library in my application. I try with a simple c file and it work. I succeded to send and receive data between the two api. But when i want to introduce an existing c library i have this kind of error when i run ndk-build.
root#xxx:~/workspace/Myproject# ndk-build
Android NDK: WARNING: APP_PLATFORM android-16 is larger than android:minSdkVersion 8 in /root/workspace/MyOpus/AndroidManifest.xml
[armeabi-v7a] Compile thumb : my_manager <= myencoder.c
[armeabi-v7a] SharedLibrary : libmy_manager.so
/usr/local/mes_installations/android_tool/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: ./obj/local/armeabi-v7a/libopus.so: incompatible target
/usr/local/mes_installations/android_tool/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi-v7a/objs/my_manager/src/myencoder.o: in function myEncodeOpus:/root/workspace/MyOpus/jni/src/myencoder.c:81: error: undefined reference to 'opus_encoder_create'
/usr/local/mes_installations/android_tool/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi-v7a/objs/my_manager/src/myencoder.o: in function myEncodeOpus:/root/workspace/MyOpus/jni/src/myencoder.c:84: error: undefined reference to 'opus_strerror'
/usr/local/mes_installations/android_tool/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi-v7a/objs/my_manager/src/myencoder.o: in function myEncodeOpus:/root/workspace/MyOpus/jni/src/myencoder.c:93: error: undefined reference to 'opus_encoder_ctl'
/usr/local/mes_installations/android_tool/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi-v7a/objs/my_manager/src/myencoder.o: in function myEncodeOpus:/root/workspace/MyOpus/jni/src/myencoder.c:96: error: undefined reference to 'opus_strerror'
/usr/local/mes_installations/android_tool/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi-v7a/objs/my_manager/src/myencoder.o: in function myEncodeOpus:/root/workspace/MyOpus/jni/src/myencoder.c:120: error: undefined reference to 'opus_encode'
/usr/local/mes_installations/android_tool/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi-v7a/objs/my_manager/src/myencoder.o: in function myEncodeOpus:/root/workspace/MyOpus/jni/src/myencoder.c:123: error: undefined reference to 'opus_strerror'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi-v7a/libmy_manager.so] Error 1
How can i solve this problem? I read all the Android.mk documentation but i did not see any thing to solve it. Regards

you said you succeed to create shared library libmy_manager . in Android.mk specify the path of libmy_manager.so and the path of header files
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmy_manager
LOCAL_SRC_FILES := libmy_manager.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include #path of header file with define
# function in libmy_manager
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := my_manager
LOCAL_SRC_FILES := my_manager.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES := libmy_manager
include $(BUILD_SHARED_LIBRARY)

Related

Android call a (not JNI) function from external .so library

I am struggling with this problem since few weeks I hope someone can help me.
I have created a C function wrapper in my Android app that impements the JNI export. I have also created a second .so library in order to call my original library but here I have a problem when linking with ndk-build. This is my code:
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := my_prebuilt_lib
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../jni/
LOCAL_SRC_FILES := ../jniLibs/$(TARGET_ARCH_ABI)/my_prebuilt_lib.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := myjni
LOCAL_SRC_FILES := wrapper.cpp
LOCAL_SHARED_LIBRARY += my_prebuilt_lib
include $(BUILD_SHARED_LIBRARY)
This is the command I am using to build myjni.so:
ndk-build NDK_PROJECT_PATH=./ NDK_APPLICATION_MK=./jni/Application.mk
And I get this error:
Android NDK: WARNING: APP_PLATFORM android-16 is higher than android:minSdkVersion 1 in .//AndroidManifest.xml. NDK binaries will *not* be compatible with devices older than android-16. See https://android.googlesource.com/platform/ndk/+/master/docs/user/common_problems.md for more information.
[armeabi-v7a] Install : my_prebuilt_lib.so => libs/armeabi-v7a/liblocSDK4d.so
[armeabi-v7a] Compile++ thumb: myjni <= wrapper.cpp
[armeabi-v7a] SharedLibrary : libmyjni.so
jni/wrapper.cpp:24: error: undefined reference to 'hex2int(char)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
Looking into my_prebuilt_lib.so I can see the following declaration about the function I want to call:
00004c51 T hex2int(char)
Any suggestion?

undefined reference to ndk boost android

I have a problem when creating an executable with ndk using the BOOST libraries in c ++ using already mentioned ndk, this is the problem
C:\Users\GENERAL\Downloads\Compressed\tcpproxy\proxy\jni>ndk-build
[armeabi] Compile++ thumb: proxy <= tcpproxy_server.cpp
[armeabi] Executable : proxy
C:/Users/GENERAL/Downloads/Compressed/tcpproxy/proxy//jni/boost_1_56_0/boost/sys
tem/error_code.hpp:322: error: undefined reference to 'boost::system::system_cat
egory()'
C:/Users/GENERAL/Downloads/Compressed/tcpproxy/proxy//jni/boost_1_56_0/boost/thr
ead/exceptions.hpp:51: error: undefined reference to 'boost::system::system_cate
gory()'
C:/Users/GENERAL/Downloads/Compressed/tcpproxy/proxy//jni/boost_1_56_0/boost/asi
o/error.hpp:225: error: undefined reference to 'boost::system::system_category()
'
C:/Users/GENERAL/Downloads/Compressed/tcpproxy/proxy//jni/boost_1_56_0/boost/asi
o/error.hpp:225: error: undefined reference to 'boost::system::system_category()
'
C:/Users/GENERAL/Downloads/Compressed/tcpproxy/proxy//jni/boost_1_56_0/boost/sys
tem/error_code.hpp:221: error: undefined reference to 'boost::system::generic_ca
tegory()'
C:/Users/GENERAL/Downloads/Compressed/tcpproxy/proxy//jni/boost_1_56_0/boost/sys
tem/error_code.hpp:222: error: undefined reference to 'boost::system::generic_ca
tegory()'
collect2.exe: error: ld returned 1 exit status
make.exe: *** [C:/Users/GENERAL/Downloads/Compressed/tcpproxy/proxy//obj/local/a
rmeabi/proxy] Error 1
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/boost_1_56_0 #patch of include BOOST
LOCAL_MODULE := proxy
LOCAL_SRC_FILES := tcpproxy_server.cpp
include $(BUILD_EXECUTABLE)
Application.mk
APP_CFLAGS += -fexceptions
APP_STL := gnustl_static
NDK_TOOLCHAIN_VERSION := 4.8
APP_CPPFLAGS := -std=c++11
that is the error that I get when compiling. already investigate but can not find the solution so please thank your answer to be able to repair it and because generating that error thanks
You may need to add the boost_system library. You can add -lboost_system to your linker flags to find related library symbols in Android.mk (Assuming that boost is built as static):
LOCAL_STATIC_LIBRARIES = -lboost_system ...
include $(BUILD_STATIC_LIBRARY)

pthread error in ndk-build

Hi
I had try to compile the following Android.mk file using ndk-build.
I got the following errors. how can I resolve this problem?
please help me.
the make file is
# ================================================== ========
# NOTE:
# when executing binary, set LD_LIBRARY_PATH in device
# ================================================== ========
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
BASEDIR := ../../..
DISTDIR := ../../../..
LOCAL_MODULE := EvaluateFingerQuality
LOCAL_ARM_MODE := arm
# Includes
LOCAL_C_INCLUDES += \
$(BASEDIR)/Common/C \
$(DISTDIR)/Include
# Source files
LOCAL_SRC_FILES := \
$(BASEDIR)/Common/C/Utils.c \
EvaluateFingerQuality.c
LOCAL_LDLIBS += \
-Wl,-z,muldefs -Wl,-rpath=$(DISTDIR)/Lib/Android_$(TARGET_ARCH_ABI) \
-L $(DISTDIR)/Lib/Android_$(TARGET_ARCH_ABI) \
-lNLicensing -lNBiometricTools -lNBiometrics -lNMedia -lNCore -lc
include $(BUILD_EXECUTABLE)
the error is
$ ndk-build
Compile arm : EvaluateFingerQuality <= Utils.c
Compile arm : EvaluateFingerQuality <= EvaluateFingerQuality.c
Executable : EvaluateFingerQuality
../../../../Lib/Android_armeabi/libNCore.so: undefined reference to `pthread_rwlock_trywrlock'
../../../../Lib/Android_armeabi/libNCore.so: undefined reference to `pthread_rwlock_rdlock'
../../../../Lib/Android_armeabi/libNCore.so: undefined reference to `pthread_rwlock_tryrdlock'
../../../../Lib/Android_armeabi/libNCore.so: undefined reference to `pthread_rwlock_wrlock'
../../../../Lib/Android_armeabi/libNCore.so: undefined reference to `dladdr'
../../../../Lib/Android_armeabi/libNCore.so: undefined reference to `pthread_rwlock_destroy'
../../../../Lib/Android_armeabi/libNCore.so: undefined reference to `pthread_rwlock_init'
../../../../Lib/Android_armeabi/libNCore.so: undefined reference to `pthread_rwlock_unlock'
collect2: ld returned 1 exit status
/cygdrive/d/android-ndk-r8-windows/android-ndk-r8/build/core/build-binary.mk:381: recipe for target `/cygdrive/d/MegaMatcher_Embedded_1_2_SDK_Trial_for_Android/Tutorials/BiometricTools/C/obj/local/armeabi/EvaluateFingerQuality' failed
make: *** [/cygdrive/d/MegaMatcher_Embedded_1_2_SDK_Trial_for_Android/Tutorials/BiometricTools/C/obj/local/armeabi/EvaluateFingerQuality] Error 1
Android supports pthreads strating from API 9 version. i got the above error due to i am using old version of NDK. to avoid this build like this "$ndk-build APP_PLATFORM=android-9 "

cocos2dx eclipse: Various "error: undefined reference to "

I'm tryng to port an iOS game to Android platform with cocos2dx 2.0.4.
The core of the game is written in c++ and we managed to run it on iOS, in fact the game is avaivable on the Apple Store.
I'm using NDK r8d and lastest SDK, but I'm encountering a lot of issues when trying to build.
My application.mk is like this:
APP_STL := stlport_shared
APP_CPPFLAGS := -frtti
NDK_TOOLCHAIN_VERSION=4.7
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
ifdef STLG
LOCAL_MODULE := stlport_stlg
else
LOCAL_MODULE := stlport
endif
LOCAL_CFLAGS += -D__ANDROID__ \
-isystem $(SYSROOT)/usr/include \
LOCAL_MODULE := game_shared
LOCAL_MODULE_FILENAME := libgame
LOCAL_CPPFLAGS += -fexceptions
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes $(LOCAL_PATH)/../../../../Box2D $(LOCAL_PATH)/../../../../cocos2dx $(LOCAL_PATH)/../../../../
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,CocosDenshion/android) \
$(call import-module,cocos2dx) \
$(call import-module,extensions)
Here's the console log (only errors displayed):
/Users/arianna/android/android-ndk-r8d/sources/cxx-stl/stlport/stlport/stl/_alloc.h:158: error: undefined reference to 'std::__node_alloc::_M_allocate(unsigned int&)'
make: Leaving directory `/Users/arianna/android/cocos2d-2.0-x-2.0.4/grannnnnnny/proj.android'
/Users/arianna/android/android-ndk-r8d/sources/cxx-stl/stlport/stlport/stl/_alloc.h:161: error: undefined reference to 'std::__node_alloc::_M_deallocate(void*, unsigned int)'
/Users/arianna/android/android-ndk-r8d/sources/cxx-stl/stlport/stlport/stl/_string.c:600: error: undefined reference to 'std::__stl_throw_length_error(char const*)'
/Users/arianna/android/cocos2d-2.0-x-2.0.4/grannnnnnny/proj.android/../../cocos2dx/actions/CCActionInterval.cpp:386: error: undefined reference to '__dynamic_cast'
/Users/arianna/android/cocos2d-2.0-x-2.0.4/grannnnnnny/proj.android/../../cocos2dx/actions/CCActionInterval.cpp:1783: error: undefined reference to '__dynamic_cast'
/Users/arianna/android/cocos2d-2.0-x-2.0.4/grannnnnnny/proj.android/../../cocos2dx/actions/CCActionInterval.cpp:1838: error: undefined reference to '__dynamic_cast'
/Users/arianna/android/cocos2d-2.0-x-2.0.4/grannnnnnny/proj.android/../../cocos2dx/actions/CCActionInterval.cpp:1906: error: undefined reference to '__dynamic_cast'
/Users/arianna/android/android-ndk-r8d/sources/cxx-stl/stlport/stlport/stl/_vector.c:41: error: undefined reference to 'std::__stl_throw_length_error(char const*)'
/Users/arianna/android/android-ndk-r8d/sources/cxx-stl/stlport/stlport/stl/_vector.c:45: error: undefined reference to 'std::__stl_throw_out_of_range(char const*)'
/Users/arianna/android/android-ndk-r8d/sources/cxx-stl/stlport/stlport/stl/_string.c:604: error: undefined reference to 'std::__stl_throw_out_of_range(char const*)'
/Users/arianna/android/android-ndk-r8d/sources/cxx-stl/stlport/stlport/stl/_vector.c:41: error: undefined reference to 'std::__stl_throw_length_error(char const*)'
./obj/local/armeabi/libcocos2d.a(CCDirector.o):CCDirector.cpp:function typeinfo for cocos2d::CCDirector: error: undefined reference to 'vtable for __cxxabiv1::__vmi_class_type_info'
/Users/arianna/android/android-ndk-r8d/sources/cxx-stl/stlport/stlport/stl/_vector.c:41: error: undefined reference to 'std::__stl_throw_length_error(char const*)'
collect2: error: ld returned 1 exit status
make: *** [obj/local/armeabi/libgame.so] Error 1
Any help much appreciated, thanks...
Try this in application.mk APP_STL := gnustl_static?
You get the above undefined reference error because you have not added the necessary libraries in Android.mk files. It looks something like this
LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../libs/$(TARGET_ARCH_ABI)) \
-lcocos2d -lGLESv1_CM -llog -lgame_logic
I am stuck with this issue even after adding the libraries. Its got something to do with setting path but I dont know how to..
If you solve this let me know

OpenCV2.4 with Android Native Activity

I am currently facing the same problem than presented in this question, except that I use the 2.4 version in stead of the 2.3.1.
I've converted native-activity (from ndk samples) to c++ and modified android.mk and application.mk.
I had the same errors (with ~Mat and so one).
When I read the answers to the question, I realised I needed to add OPENCV_INSTALL_MODULES:=on and OPENCV_LIB_TYPE:=STATIC (which is strange since I have an other project with native openCV which does not require theese two lines).
But it's still not working.
Android.mk is the following :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
OPENCV_INSTALL_MODULES:=on
include includeOpenCV.mk
ifeq ("$(wildcard $(OPENCV_MK_PATH))","")
#try to load OpenCV.mk from default install location
include $(TOOLCHAIN_PREBUILT_ROOT)/user/share/OpenCV/OpenCV.mk
else
include $(OPENCV_MK_PATH)
endif
LOCAL_MODULE := native-activity
LOCAL_SRC_FILES := main.cpp engine.cpp
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES += android_native_app_glue
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
I'm getting the following errors :
./obj/local/armeabi-v7a/libopencv_core.a(persistence.cpp.o): In function `icvPuts(CvFileStorage*, char const*)':
persistence.cpp:(.text._ZL7icvPutsP13CvFileStoragePKc+0x20): undefined reference to `gzputs'
./obj/local/armeabi-v7a/libopencv_core.a(persistence.cpp.o): In function `icvGets(CvFileStorage*, char*, int)':
persistence.cpp:(.text._ZL7icvGetsP13CvFileStoragePci+0x26): undefined reference to `gzgets'
./obj/local/armeabi-v7a/libopencv_core.a(persistence.cpp.o): In function `icvXMLSkipSpaces(CvFileStorage*, char*, int)':
persistence.cpp:(.text._ZL16icvXMLSkipSpacesP13CvFileStoragePci+0x1e2): undefined reference to `gzgets'
persistence.cpp:(.text._ZL16icvXMLSkipSpacesP13CvFileStoragePci+0x208): undefined reference to `gzeof'
persistence.cpp:(.text._ZL16icvXMLSkipSpacesP13CvFileStoragePci+0x33c): undefined reference to `gzeof'
./obj/local/armeabi-v7a/libopencv_core.a(persistence.cpp.o): In function `_ZL16icvYMLSkipSpacesP13CvFileStoragePcii.clone.3':
persistence.cpp:(.text._ZL16icvYMLSkipSpacesP13CvFileStoragePcii.clone.3+0x124): undefined reference to `gzgets'
persistence.cpp:(.text._ZL16icvYMLSkipSpacesP13CvFileStoragePcii.clone.3+0x170): undefined reference to `gzeof'
/cygdrive/d/soft/NVPACK/android-ndk-r7c/build/core/build-binary.mk:366: recipe for target `obj/local/armeabi-v7a/libnative-activity.so' failed
persistence.cpp:(.text._ZL16icvYMLSkipSpacesP13CvFileStoragePcii.clone.3+0x28e): undefined reference to `gzeof'
./obj/local/armeabi-v7a/libopencv_core.a(persistence.cpp.o): In function `cvReleaseFileStorage':
persistence.cpp:(.text.cvReleaseFileStorage+0x24): undefined reference to `gzclose'
./obj/local/armeabi-v7a/libopencv_core.a(persistence.cpp.o): In function `cvOpenFileStorage':
persistence.cpp:(.text.cvOpenFileStorage+0x66e): undefined reference to `gzopen'
persistence.cpp:(.text.cvOpenFileStorage+0x8fc): undefined reference to `gzclose'
persistence.cpp:(.text.cvOpenFileStorage+0xaea): undefined reference to `gzrewind'
persistence.cpp:(.text.cvOpenFileStorage+0xbee): undefined reference to `gzrewind'
persistence.cpp:(.text.cvOpenFileStorage+0xc0a): undefined reference to `gzclose'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi-v7a/libnative-activity.so] Error 1
Is there anything I missed ?
Although this is an old question, I run into the same issue and fixed it by adding
LOCAL_LDLIBS += -lz # Compression library
To my Android.mk
Try to replace
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM
with
LOCAL_LDLIBS += -llog -landroid -lEGL -lGLESv1_CM
I ended trying with the native plasma project (in which the display is done by writing in a bitmap) and it worked well.
Maybe GLES was messing with OpenCV. I don't think there is any other difference between the two projects.
In a way the problem is solved. Still, if anyone encounters the same issue and solved it, I would be interested to understand what caused it and how to fix it.

Categories

Resources