Android NDK:linking static library with shared library - android

Iam trying to compile my native code. Here is my android.mk file
//part1-static lib
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := irrlicht
IRRLICHT_LIB_NAME := lib$(LOCAL_MODULE).a
LOCAL_C_INCLUDES := include
LOCAL_SRC_FILES := \
CColorConverter.cpp \
CCSMLoader.cpp \
CCubeSceneNode.cpp \
CD3D8Driver.cpp \
include $(BUILD_STATIC_LIBRARY)
//part-2 shared lib
include $(CLEAR_VARS)
LOCAL_MODULE := irrlichttest
LOCAL_SRC_FILES := test-app.cpp test.cpp android-receiver.cpp
LOCAL_C_INCLUDES := include
LOCAL_CFLAGS := -O3 -DANDROID_NDK -DDISABLE_IMPORTGL -I$(LOCAL_PATH)/../include/ - I./include/
LOCAL_CPPFLAGS := -Wno-error=format-security
LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog -lGLESv2
LOCAL_STATIC_LIBRARIES := irrlicht
include $(BUILD_SHARED_LIBRARY)
and here is my application.mk
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-10
APP_MODULE := irrlicht irrlichttest
i want to compile "irrlicht" module first and then "irrlichttest' module. The problem iam facing is my irrlichttest module compile first and it start looking for reference and give me undefined reference error.Right now If i compile part1(static lib) only it successfully generate libirrlicht.a, But with part2 it start giving me error. What am i doing wrong.

You have one extra \ at the end of your first LOCAL_SRC_FILES definition, this makes the 'include $(BUILD_STATIC_LIBRARY)' part go into LOCAL_SRC_FILES, and the line is never parsed / executed. In other words, due to this your module definition for the 'iirlicht' module is completely ignored by ndk-build, hence the problem you're seeing.
Remove the \ after CD3D8Driver.cpp, and that should fix it.

NDK will compile the irrlichttest sources, then irrlich sources, then create libirrlich.a, and only after that it will link libirrlichttest.so. It is very insightful to run
ndk-build clean all V=1
and see in the build log which commands are actually executed to build the project.

Related

Android NDK module that is dependant on another module

I have an android project written using the Android NDK. Within the project, I am using two prebuilt shared libraries(GpkgSDK and spatialite) and building two more shared libraries(WFSHelpers and com_example_gpkgviewer_jni_WKTConverter). The only library that interacts with the Java level of the application is the com_example_gpkgviewer_jni_WKTConverter library.
The dependencies between the libraries are shown below:
WFSHelpers is dependent on GpkgSDK and spatialite
com_example_gpkgviewer_jni_WKTConverter is dependent on WFSHelpers
The issue that I am having is that when I try to run ndk-build, I am getting a lot of undefined references when attempting to build the com_example_gpkgviewer_jni_WKTConverter library. The other libraries are successfully built. The way I would normally resolve these undefined references is by including the following in my com_example_gpkgviewer_jni_WKTConverter module definition :
LOCAL_SHARED_LIBRARY := WFSHelpers
I am unsure as to whether I would also need to include the libraries that the WFSHelpers is dependent on like so:
LOCAL_SHARED_LIBRARY := WFSHelpers GpkgSDK spatialite
I have also tried them in a different order like so but it does not seem to resolve my issue:
LOCAL_SHARED_LIBRARY := GpkgSDK spatialite WFSHelpers
My Application.mk is included below:
NDK_TOOLCHAIN_VERSION := 4.8
# APP_STL := stlport_shared --> does not seem to contain C++11 features
APP_STL := gnustl_shared
# Enable c++11 extentions in source code
APP_CPPFLAGS += -std=c++11
APP_CPPFLAGS += -frtti
APP_CPPFLAGS += -fexceptions
APP_MODULES := GpkgSDK spatialite WFSHelpers com_example_gpkgviewer_jni_WKTConverter
APP_ABI := armeabi armeabi-v7a
My Android.mk is shown below:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := GpkgSDK
LOCAL_SRC_FILES := libMP.so
LOCAL_EXPORT_C_INCLUDES := \
$(LOCAL_PATH)/include \
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := spatialite
LOCAL_SRC_FILES := spatialamal/prebuilt/$(TARGET_ARCH_ABI)/libspatialite.so
LOCAL_EXPORT_C_INCLUDES := spatialamal/headers/spatialite \
spatialamal/headers
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := WFSHelpers
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
LOCAL_SRC_FILES := \
GPKGReader/Debug.h \
GPKGReader/DLLExport.h \
GPKGReader/DBQueryResult.cpp \
GPKGReader/GeoPackageDB.cpp \
GPKGReader/GPKGReader.cpp \
GPKGReader/order32.h \
GPKGReader/SpecDefinitions.h \
GPKGReader/WKBGenericGeometry.cpp \
GPKGReader/WKBLineString.cpp \
GPKGReader/WKBMultiLineString.cpp \
GPKGReader/WKBMultiPolygon.cpp \
GPKGReader/WKBPoint.cpp \
GPKGReader/WKBPolygon.cpp \
GPKGDataLayer/GPKGDataLayer.cpp
LOCAL_SHARED_LIBRARIES := GpkgSDK spatialite
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
LOCAL_MODULE := com_example_gpkgviewer_jni_WKTConverter
LOCAL_SRC_FILES := com_example_gpkgviewer_jni_WKTConverter.cpp
LOCAL_SHARED_LIBRARY := WFSHelpers GpkgSDK spatialite
include $(BUILD_SHARED_LIBRARY)
An example of the undefined reference errors that I am getting is shown below:
[armeabi] SharedLibrary : libcom_example_gpkgviewer_jni_WKTConverter.so
jni/com_example_gpkgviewer_jni_WKTConverter.cpp:59: error: undefined reference
o 'WKBGenericGeometry::readInt32(unsigned char, unsigned char*, unsigned int)'
collect2.exe: error: ld returned 1 exit status
make.exe: *** [obj/local/armeabi/libcom_example_gpkgviewer_jni_WKTConverter.so]
Error 1
The line of code within com_example_gpkgviewer_jni_WKTConverter.cpp that causes this error is shown below:
*id_arg = WKBGenericGeometry::readInt32(byte_order, &(bytes[4]), length - 4);
Any suggestions on how I can resolve the dependencies.
After attempting to get this to build for ages, I have finally been able to get it to work. Although, I am not quite sure what the differences are, so anyone who can shed a light on this is welcome to do so. My issue was that I had dependencies on other libraries. The documentation states the following:
LOCAL_SHARED_LIBRARIES
The list of shared libraries modules this
module depends on at runtime. This is necessary at link time and to
embed the corresponding information in the generated file.
LOCAL_LDLIBS
The list of additional linker flags to be used when building your shared library or executable. This is useful to pass the
name of specific system libraries with the '-l' prefix. For example,
the following will tell the linker to generate a module that links to
/system/lib/libz.so at load time:
LOCAL_LDLIBS := -lz
See STABLE-APIS for the list of exposed system libraries you can linked against with this NDK release.
NOTE: This is ignored for static libraries, and ndk-build will print a warning if
you define it in such a module.
Thus in my Android.mk file, I had to use LOCAL_LDLIBS instead of LOCAL_SHARED_LIBRARIES to indicate the dependencies.
My new Android.mk is as shown below:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := GpkgSDK
LOCAL_SRC_FILES := libMP.so
LOCAL_EXPORT_C_INCLUDES := \
$(LOCAL_PATH)/include \
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := spatialite
LOCAL_SRC_FILES := spatialamal/prebuilt/$(TARGET_ARCH_ABI)/libspatialite.so
LOCAL_EXPORT_C_INCLUDES := spatialamal/headers/spatialite \
spatialamal/headers
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := WFSHelpers
LOCAL_SRC_FILES := \
GPKGReader/Debug.h \
GPKGReader/DLLExport.h \
GPKGReader/DBQueryResult.cpp \
GPKGReader/GeoPackageDB.cpp \
GPKGReader/GPKGReader.cpp \
GPKGReader/order32.h \
GPKGReader/SpecDefinitions.h \
GPKGReader/WKBGenericGeometry.cpp \
GPKGReader/WKBLineString.cpp \
GPKGReader/WKBMultiLineString.cpp \
GPKGReader/WKBMultiPolygon.cpp \
GPKGReader/WKBPoint.cpp \
GPKGReader/WKBPolygon.cpp \
GPKGDataLayer/GPKGDataLayer.cpp
LOCAL_LDLIBS := libs/$(TARGET_ARCH_ABI)/libGpkgSDK.so
LOCAL_LDLIBS += libs/$(TARGET_ARCH_ABI)/libspatialite.so
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := com_example_gpkgviewer_jni_WKTConverter
LOCAL_SRC_FILES := com_example_gpkgviewer_jni_WKTConverter.cpp
LOCAL_LDLIBS := libs/$(TARGET_ARCH_ABI)/libWFSHelpers.so
LOCAL_LDLIBS += libs/$(TARGET_ARCH_ABI)/libGpkgSDK.so
include $(BUILD_SHARED_LIBRARY)
I shall leave this answer open for a while as I am not entirely sure what the difference between LOCAL_LDLIBS and LOCAL_SHARED_LIBRARIES is. If anyone can provide me with an explanation, please do. If not, I shall mark this answer as accepted after giving it some time. Thanks !

NativeActivity using C++

I'm trying to only use c++ to porting all my apps on Android devices from IOS and from ANDROID with JAVA EGL.
One thing I just met is "fatal error: android_native_app_glue.h: No such file or directory" so I'm going over my make file , 'Android.mk' below
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_DEFAULT_CPP_EXTENSION := cpp
LOCAL_MODULE := StarEngine
LOCAL_SRC_FILES := \
main.cpp \
StarEngine.cpp \
../../../StarEngine/StarShader.cpp \
../../../StarEngine/StarTexture.cpp \
../../../StarEngine/StarFBO.cpp\
../../../StarEngine/StarTimer.cpp\
../../../StarEngine/StarMath/Matrix.cpp\
../../../StarEngine/StarMath/Random.cpp\
../../../StarEngine/StarMath/Transform.cpp\
../../../StarEngine/StarMath/Vector.cpp\
../../../StarEngine/StarMath/neonmath/neon_matrix_impl.cpp\
../../../StarEngine/StarMath/vfpmath/matrix_impl.cpp\
../../../StarEngine/StarMath/vfpmath/utility_impl.cpp\
#../../../StarEngine/StarSound/StarSound.cpp
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
LOCAL_CFLAGS := -D_ARM_ARCH_7=1
LOCAL_CPPFLAGS := -D_ARM_ARCH_7=1
else
endif
LOCAL_CFLAGS := -DCONFIG_EMBEDDED -DUSE_IND_THREAD -marm -mfpu=neon -mfloat-abi=softfp
APP_STL := stlport_static
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -ldl -lGLESv2 -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := android_native_app_glue cpufeatures
include $(BUILD_SHARED_LIBRARY)
$(call import-module,cpufeatures,android/native_app_glue)
Answer :
When you use more than two static libraries you should put import-module more than twice like this
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
$(call import-module,cpufeatures)
According to the reference in NDK folder :
import-module
A function that allows you to find and include the Android.mk
of another module by name. A typical example is:
$(call import-module,<name>)
And this will look for the module tagged <name> in the list of
directories referenced by your NDK_MODULE_PATH environment
variable, and include its Android.mk automatically for you.

Prebuild of static library failed

I try to set up an Android NDK build based on CMake scripts, which dynamically create the required Android make files. While I can't use the JNI folder structure I split the build process in several separated make scripts:
1st Create root Android.mk file located in project root:
#ANDROID ROOT MAKEFILE
LOCAL_PATH := D:/binrev/repository/bar
include $(CLEAR_VARS)
MY_LOCAL_CFLAGS := -DDEBUG
include D:/binrev/repository/bar/src/Android.mk
2nd Create source Android.mk file in project source folder and perform module build:
$(info "[INFO] Source Makefile invoked")
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_C_INCLUDES:= D:/binrev/repository/bar/include
LOCAL_SRC_FILES := bar.cpp
ifeq (debug,"debug")
MY_LOCAL_CFLAGS := -DDEBUG
endif
ifeq (false,true)
LOCAL_ARM_MODE := arm
endif
LOCAL_EXPORT_C_INCLUDES := D:/binrev/repository/bar/include
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += -landroid
LOCAL_STATIC_LIBRARIES += foo
ifeq (OFF, ON)
include $(BUILD_SHARED_LIBRARY)
else
include $(BUILD_STATIC_LIBRARY)
endif
Basicly this mechanism works and I could compile my sources, but I fail if I try to include a Prebuild of a library. I tried the following ways to include a pre-build
of a static library (with modified source/include definitions):
include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := lib/android/$(TARGET_ARCH_ABI)/libfoo.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)
1st Prebuild definition in source Android.mk file
2nd Call import-module mechanism and add Prebuild Android.mk file to prebuild-lib
3rd Prebuild definition in root Android.mk file
[Edit:] Here is the snipped of the call-import test which also fail:
$(info "[INFO] Source Makefile invoked")
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_C_INCLUDES:= D:/binrev/repository/bar/include
LOCAL_SRC_FILES := bar.cpp
ifeq (debug,"debug")
MY_LOCAL_CFLAGS := -DDEBUG
endif
ifeq (false,true)
LOCAL_ARM_MODE := arm
endif
LOCAL_EXPORT_C_INCLUDES := D:/binrev/repository/bar/include
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += -landroid
LOCAL_STATIC_LIBRARIES += foo
ifeq (ON, ON)
include $(BUILD_SHARED_LIBRARY)
else
include $(BUILD_STATIC_LIBRARY)
endif
$(call import-module, external-deps/foo)
In each case the Script with the prebuild-definition is invoked, but the prebuild
is not performed. When my NDK build has been compleded, the prebuild library and
objects are not copied to my obj folder. It seems to me that the prebuild is
completely ignored. But the path to prebuild sources are correct, otherwise the
compile fails with missing file error.
You could get the complete source of this test implementation here:
[Test projects][1]https://sourceforge.net/projects/binrevengine/files/publications/
Hint: The bar project is the project which tries to prebuild the foo project.
The foo project contains the prebuild sources.
The added tests projects could be build by your own using MinGW64 with GCC 4.7/4.8 in handshake with CMake and pre installed NDK (using r8e).
I completly get lost and running out of ideas ...
Thanks for any help.
The Android build system will not build a static library without it being used by a shared library. Just create a dummy shared library that has your static library as a dependency and voila:
include $(CLEAR_VARS)
LOCAL_MODULE := dummy
LOCAL_PATH := $(LOCAL_PATH)
LOCAL_SRC_FILES := dummy.c
LOCAL_STATIC_LIBRARIES := foo
include $(BUILD_SHARED_LIBRARY)
To exclude possible sources of defects I've reduced the Android make file to simplest case without using CMake generator of those files:
LOCAL_PATH := D:/binrev/repository/bar
include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := external-deps/foo/lib/android/$(TARGET_ARCH_ABI)/libfoo.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_C_INCLUDES:= D:/binrev/repository/bar/include
LOCAL_C_INCLUDES+= D:/binrev/repository/bar/external-deps/foo/include
LOCAL_SRC_FILES := src/bar.cpp
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += -landroid
LOCAL_SHARED_LIBRARIES := foo-prebuilt
include $(BUILD_SHARED_LIBRARY)
and:
LOCAL_PATH := D:/binrev/repository/foo
include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_C_INCLUDES:= D:/binrev/repository/foo/include
LOCAL_SRC_FILES := src/foo.cpp
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += -landroid
include $(BUILD_STATIC_LIBRARY)
The failure still exists. The prebuild of the foo library is not executed. I also excluded MinGW64 as possible source of defect, if I try to build the project with Windows command line it results in same issue. The shared library is build, but the prebuild is not executed.
I checked my sources and scripts multiple times, but can't find any failure.
Any ideas what could be wrong or missing?

include libjpeg in a NDK build

As specified in this answer, I downloaded libjpeg 8d from github and placed it in a folder {ANDROID_APP}/jni/libjpeg. This library has it's own Android.mk, so I tried to include it at the end of my {ANDROID_APP}/jni/Android.mk this way :
include $(LOCAL_PATH)/libjpeg/Android.mk
Note : I'm using the latest version of android NDK (r8c)
After running ndk-build, I still get this error :
ANDROID_APP/jni/libfoo/foo_analysis.c:36:21: fatal error: jpeglib.h: No such file or directory
This is the structure of my global Android.mk :
LOCAL_PATH := $(call my-dir)
# libFoo
include $(CLEAR_VARS)
LOCAL_MODULE := libfoo
LOCAL_MODULE_FILENAME := libfoo
LOCAL_SRC_FILES := libfoo/foo.c libfoo/foo_analysis.c libfoo/foo_extract.c
LOCAL_STATIC_LIBRARIES := libbmp # declared previously but not shown in this example
LOCAL_CFLAGS = ${FLAGS}
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/libfoo
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)
# libBar
include $(CLEAR_VARS)
LOCAL_MODULE := libbar
LOCAL_MODULE_FILENAME := libbar
LOCAL_SRC_FILES := bar/bar.c
LOCAL_STATIC_LIBRARIES := libfoo
LOCAL_CFLAGS = ${FLAGS}
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)
# callbar
LOCAL_MODULE := libcallbar
LOCAL_MODULE_FILENAME := libcallbar
LOCAL_SRC_FILES := com_androidapp_nativeC_callbar.c
LOCAL_STATIC_LIBRARIES := libbar
LOCAL_CFLAGS = ${FLAGS}
include $(BUILD_SHARED_LIBRARY)
#libjpeg
include $(LOCAL_PATH)/libjpeg/Android.mk
I tried to use LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/libfoo $(LOCAL_PATH)/libjpeg
and LOCAL_C_INCLUDES := $(LOCAL_PATH)/libjpeg in the libFoo module, but I still get the same error.
Just looked at Android.mk in jpeg8d-master folder and seems to be it has nothing.
I was trying to compile library directly according to STANDALONE-TOOLCHAIN.HTML
I do next: $export NDKROOT=/home/alex/tools/android-ndk-r8c (where is your NDK) $export SYSROOT=$NDKROOT/platforms/android-9/arch-arm (or any other android platform)
but files from jpeg8d-master have windows \r symbols and I deleted config.guess, config.sub, depcomp than use $automake -a command. And replace ltmain.sh from glib-2.34.0
than $./configure CC="$NDKROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc --sysroot=$SYSROOT" --host=arm-linux-androideabi $make
next try prebuilts feature NDK - How to use a generated .so library in another project and NDK/PREBUILTS.HTML

ndk-build failed: No rule to make target pjsua_wrap.cpp needed by pjsua_wrap.o

I have been trying to build the CSipSimple in my Win7 (X64) machine for couple of days.
I am using cygwin for building the code. But currently it is displaying me the below image.
Please give me some suggestions.
It would be useful if you showed us your "android.mk" file. Here is an example of an android.mk file which builds a library called "mylib" from 2 cpp files:
LOCAL_PATH := $(call my-dir)
LOCAL_ARM_MODE :=arm
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := \
mysource1.cpp \
mysource2.cpp \
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -lz -lm -llog
include $(BUILD_SHARED_LIBRARY)

Categories

Resources