为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;
} |
#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));
} |
#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目录下,才成功跑起来。