2015/10/26

Cross-compiling a C application using the Android NDK

1 download ndk
export NDK=~/Downloads/android-ndk-r10e

2 Set cross-compiler for x86 (emulator)
mac:
export NDK_TOOLCHAIN=${NDK}/toolchains/x86-4.8/prebuilt/darwin-x86_64/bin/i686-linux-android-
export NDK_SYSROOT=${NDK}/platforms/android-19/arch-x86

Set cros-compiler for arm (device)
export NDK_TOOLCHAIN=${NDK}/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-
export NDK_SYSROOT=${NDK}/platforms/android-19/arch-arm

3 compile
// hello-world.c
#include <stdio.h>
int main(void)
{
printf("Hello world cross compiled on Android!\n");
 return 0;
}

make CC=${NDK_TOOLCHAIN}gcc CFLAGS=--sysroot=${NDK_SYSROOT} hello-world

4 push to test
$ adb push hello-world /data/

$ adb shell

# /data/hello-world

Hello world cross compiled on Android!

reference:
https://yaapb.wordpress.com/2012/09/27/cross-compiling-a-c-application-using-the-android-ndk/

没有评论: