为android 生成 native 程序

四 11th, 2010

 

为android生成原生应用程序,不难啊。
初始条件:安装android的ndk,比如目前最新的 android-ndk-r3。(2010-03版)
我的目录结构:
~/android-ndk-r3
~/android-ndk-r3/temp  这个是当前工作目录。

Step1: 写两个文件:
hello.c:

#include <stdio.h>
 
int main()
{
    printf("hello, world\n");
    return 0;
}

start.c

#include <stdlib.h>
extern int main(int argc, char **argv);
 
void _start(int argc, char **argv)
{
    exit (main (argc, argv));
}

Step2: compile
[code]
../build/prebuilt/linux-x86/arm-eabi-4.2.1/bin/arm-eabi-gcc \
-I ../build/platforms/android-3/arch-arm/usr/include/  -c hello.c
../build/prebuilt/linux-x86/arm-eabi-4.2.1/bin/arm-eabi-gcc \
-I ../build/platforms/android-3/arch-arm/usr/include/  -c start.c
[/code]

Step3: link
[code]
../build/prebuilt/linux-x86/arm-eabi-4.2.1/bin/arm-eabi-ld --entry=_start \
--dynamic-linker /system/bin/linker -nostdlib     -rpath /system/lib  \
-L ../build/platforms/android-3/arch-arm/usr/lib/  \
-rpath ../build/platforms/android-3/arch-arm/usr/lib/    \
-lc -o hello hello.o start.o
[/code]

Step4: execute:
[code]
adb push hello /data/hello
adb shell
cd /data
chmod 755 ./hello
./hello
hello, world
[/code]

值得注意的一点是,在/sdcard目录下,程序运行不起来。我一开始还以为是没编好呢。后来扔到/data目录下,才成功跑起来。

标签:
  1. paranoia
    四 11th, 201019:19

    李总这篇写的一般般啊,期待牛文

  2. admin
    四 11th, 201020:00

    囧,你想要啥样的牛文。 这年头牛文很难了,记下一些东西好让以后可查,就够了。

  3. Ajrin
    十二 13th, 201109:28

    This is a really good read for me. Must agree that you are one of the coolest blogger I ever saw. Thanks for posting this useful information. This was just what I was on looking for. I’ll come back to this blog for sure!

    african mango

  4. Buy Facebook Fans
    十二 31st, 201106:23
    #4
*