以下是详细步骤:

  1. 下载NDK,解压至/usr/loca/android-ndk-r3
  2. 写hello.c

    root@ubuntu8:~/samples# cat hello.c
    #include <stdio.h>

    int main(){
        printf("%sn", "hello, world!");
        return 0;
    }

  3. 生成hello.o
    root@ubuntu8:~/samples# arm-eabi-gcc -c hello.c -I/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/include                           
    root@ubuntu8:~/samples#
  4. 生成hello
    NDKLIB=/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib/

    root@ubuntu8:~/samples# arm-eabi-gcc -nostdlib -ldl -lc -o hello hello.o $NDKLIB/crtbegin_dynamic.o $NDKLIB/crtend_android.o -L$NDKLIB -Wl,-dynamic-linker,/system/bin/linker
    root@ubuntu8:~/samples#
    对链接选项,分成几组,他们分别解决3个问题:

    • -nostdlib $NDKLIB/crtbegin_dynamic.o $NDKLIB/crtend_android.o
      否则link hello时,报crt0.o找不到
    • -ldl –lc -L$NDKLIB
      否则link hello时,报libdl和libc找不到
    • -Wl,-dynamic-linker,/system/bin/linker
      否则run hello时,报“
      ./hello: not found”这么一个错

其他,Android NDK提供了一个很小的so环境,如果要将其他linux program移植至android,而该program又依赖于很多so,那么这些被依赖的so也必须移植至Android。那么Android NDK提供的so有哪些呢,请看:

/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib/libGLESv2.so
/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib/libGLESv1_CM.so
/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib/libc.so
/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib/libdl.so
/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib/libz.so
/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib/libthread_db.so
/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib/liblog.so
/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib/libm.so
/usr/local/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib/libstdc++.so

一共9个so被include至NDK中,很显然,这是一个非常有限的集合。

Advertisements