Question

I have native source code written in C that I would like to run on my Android device (Nexus 7). I already successfully did lots of research and online tutorials on running native code on Android using Android NDK. I gained quite some knowledge on this. However, the code that I have makes use of the complex functionalities of the standard math library, defined in complex.h. The NDK C library however does not seem to support the complex functionalities. Whenever I do an ndk-build on the project I get:

fatal error: complex.h: no such file or directory.

As a solution I thought of getting the standard math library (libm.a) from arm-linux-gnueabi and link it with my native source. Here is my Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := StandardC
LOCAL_SRC_FILES := libc.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := mathLib
LOCAL_SRC_FILES := libm.a
LOCAL_STATIC_LIBRARIES := StandardC
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := ComplexOperations
LOCAL_SRC_FILES := libComplexOperations.a
LOCAL_STATIC_LIBRARIES := mathLib
LOCAL_C_INCLUDES += /usr/arm-linux-gnueabi/include
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := MySource
LOCAL_CFLAGS += -std=c99
LOCAL_SRC_FILES := com_samuel_test_ComplexOperationsLib.c
LOCAL_C_INCLUDES += /usr/arm-linux-gnueabi/include
LOCAL_STATIC_LIBRARIES := ComplexOperations
include $(BUILD_SHARED_LIBRARY)

I had to link the libc of the arm-linux-gnueabi-gcc as well as libm needs it. The "ComplexOperations" module was statically compiled using arm-linux-gnueabi-gcc with compiler flags -march=armv7-a. This library makes use of complex.h. This builds without any errors and warnings. But when I run the application and call

System.loadLibrary("MySource");

I get this error on logcat:

E/dalvikvm( 3932): dlopen("/data/app-lib/com.samuel.test-1/libMySource.so") failed: Cannot load library: soinfo_relocate(linker.cpp:975): cannot locate symbol ".LANCHOR2" referenced by "libMySource.so"...

On this error an UnsatisfiedLinkError exception is thrown which causes the application to crash if not handled.

Can somebody PLEAASEE help me out!! I've already tried figuring this out myself for days!! :(

Était-ce utile?

La solution 4

SOLVED!

First of all I should've built my ComplexOperations sources with the toolchain provided by the Android NDK, as the documentation clearly states not to cross-compile with gnu compilers. So that was my first mistake. That alone didn't yet solve my problem as I still got the complex.h not found error when I do an ndk-build. The Android NDK contains a VERY limited implementation of the Standard libc. To solve this I used CrystaX NDK, an extended implementation of Android NDK. That solved everything!!

Autres conseils

As of Android-L(ollipop) the NDK now provides the complex.h header.

Download the latest NDK revision from https://developer.android.com/tools/sdk/ndk/index.html and complex.h can be found in /platforms/android-L/arch-arm/usr/include.

I added the following line to Android.mk, which seems to have fixed the problem.

LOCAL_C_INCLUDES += C:\Users\Sami\workspace\android-ndk-r8e\sources\cxx-stl\gnu-libstdc++\4.7\include\

Not sure which variable would replace the C:\Users\Sami\workspace\ part, so if anybody know, please do tell.


Edit: Actually, that only removed the error for some reason, but it didn't fix the problem from what I noticed. On that note, C:\Users\Sami\workspace\android-ndk-r8e could have been replaced with $(NDK_ROOT).

I tried Crystax's NDK, but I kept getting the complex.h not being found error. All I did was simply copying complex.h and _mingw.h into my jni directory. Everything worked an it the code was tested on both x86 and ARM emulator.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top