How can I use complex.h for Android NDK?
Asked Answered
D

4

8

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!! :(

Dieter answered 6/3, 2013 at 14:50 Comment(1)
Android seriously doesn’t have a complex.h?Nickolenicks
D
0

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!!

Dieter answered 22/3, 2013 at 8:9 Comment(6)
How did you manage to solve the problem? You just used the cross-compiler from CrystaX?Bedazzle
It's been a while back, but if I look at my answer above I used Android NDK's toolchain to cross-compile and used CrystaX's Ndk-build tool. Maybe the ndk-build tool of Android NDK does support the complex.h library now. But if not, the CrystaX will do. Good luck!Dieter
Seems that only the g++ has the complex.h file. I'm also considering the CrystaX cross-compilers. Thx Anyway.Bedazzle
Did you try Sami Al-Terkawi Hasib's sollution as he described in his answer? He seemed to have it solved without CrystaXDieter
The "complex.h" files from these NDK seems very limited... I solved the problem in a different way. Thx for your help.Bedazzle
@Bedazzle .. can you please share how did you solve the problemHough
C
3

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.

Cordiality answered 16/10, 2014 at 21:50 Comment(0)
D
1

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.

Declivitous answered 10/7, 2013 at 21:15 Comment(0)
M
0

It seems like your PATH is broken. Kindly refer to following posts to solve this issue.

Android NDK: how to include Android.mk into another Android.mk (hierarchical project structure)?

android ndk error "no such file or directory"?

Michaud answered 8/3, 2013 at 5:57 Comment(1)
Thanks Yasir. But my PATH is not broken as the compiler can find all other headers. It's just that the compiler that NDK uses does not support complex operations which should've been defined in the math lib. I guess I already solved that problem by linking the bionic math lib with my source, but now I bumped into the linking error problem! I've done an objdump on the libraries and learnt that .LANCHOR2 is defined in libc.a which I linked my source with (see my Android.mk). I guess I should find a way now to load that libc to android as wellDieter
D
0

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!!

Dieter answered 22/3, 2013 at 8:9 Comment(6)
How did you manage to solve the problem? You just used the cross-compiler from CrystaX?Bedazzle
It's been a while back, but if I look at my answer above I used Android NDK's toolchain to cross-compile and used CrystaX's Ndk-build tool. Maybe the ndk-build tool of Android NDK does support the complex.h library now. But if not, the CrystaX will do. Good luck!Dieter
Seems that only the g++ has the complex.h file. I'm also considering the CrystaX cross-compilers. Thx Anyway.Bedazzle
Did you try Sami Al-Terkawi Hasib's sollution as he described in his answer? He seemed to have it solved without CrystaXDieter
The "complex.h" files from these NDK seems very limited... I solved the problem in a different way. Thx for your help.Bedazzle
@Bedazzle .. can you please share how did you solve the problemHough

© 2022 - 2024 — McMap. All rights reserved.