android-ndkstatic-linkinglibm

Error executing native application compiled with NDK in Android. cannot locate 'log2' (Works OK on emulator)


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?


Solution

  • 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.