1

I (cross)compiled a binary using Android-NDK with no errors during the compilation/linking phase. The application is statically linked.

Using a Nesux7 emulator I execute:

 $ adb shell mount -o remount,rw /system
 $ adb push myApp /system/bin
 $ adb shell /system/bin/myApp

It works "OK" and prints the correct output.

Then I create the final apk and install on a real phone. It fails to execute. Debugging the app:

 $ adb shell
 $ run-as com.blablabla.myapp
 $ cd /data/data/com.blablabla.myapp/files/bin
 $ ./myApp

next error is raised:

reloc_library[1306]: 21538 cannot locate 'log2'...
CANNOT LINK EXECUTABLE

On another post (NDK: libm static linking) somebody complains about differences between libm.a and libm.so, but anyway, since it works on my emulator, I think the compilation is "sort of OK".

I'm completly stuck at this point. Any idea?

Community
  • 1
  • 1
earizon
  • 2,099
  • 19
  • 29

1 Answers1

4

Finally I found the source of problem.

I was using /opt/android-ndk-r9d/platforms/android-19 as NDK. This version already includes log2 and other math functions in the standard libm platform, so there were not problems during compilation or executing in devices/emulators using this version of Android.

Older versions do not include such functions so an error is raised at runtime even if compilation is "OK". Using a older platform /opt/android-ndk-r9d/platforms/android-3) the source code (avconv/ffmpeg) detected it and replaced it with custom macros in libavutil/libm.h. This warrants that it will work in any Android version, fixing the compilation/running problem.

There is not lot of documentation about what's included in each platform/android-N. The best source of info I could find about "batteries" included in different NDK platforms is:

http://mobilepearls.com/labs/native-android-api/ndk/docs/STABLE-APIS.html

that anyway does not provide lot of details.

earizon
  • 2,099
  • 19
  • 29
  • I'm having the same problem. If I compile with android-19+ everything goes well during compilation but it crashes at runtime. If I compile with android-x (where there's no "log2f" defined) I get a warning message during compilation: "incompatible declaration of built-in function log2f" and again at runtime I get the same error. Any ideas? – Traian May 10 '15 at 19:21
  • 1
    @unichiduci Use android-3. Your C code must also check (through #ifdef macros) that log2f is not defined anywhere and replace it with its own custom version. That's the case with ffmpeg. It will use its own math lib to fix the bug: https://www.ffmpeg.org/doxygen/2.1/libm_8h.html . Compiling with newer android versions ffmpeg will detect that the log2f function is already provided by an external library and link to it. In such case it will work only in newer Android devices. NDK is not so clean as using the Java Front End that hides all those complexities. – earizon May 15 '15 at 11:51
  • #define log2f(x) (logf(x)/logf(2.0f)) problem solved :). – NoAngel Aug 30 '16 at 10:55