Android是基于Linux的操作系統(tǒng),處理器是ARM的,所以要在Linux或Windows等x86系統(tǒng)上編譯Android能運行的程序,你需要一個交叉編譯器。
在Linux下面,你可以自已編譯一個交叉編譯環(huán)境,但Windows下面,就比較復(fù)雜(也可以在cygwin中編譯一個),但你可以選擇下載一個現(xiàn)成的交叉編譯環(huán)境:
http://www.codesourcery.com/gnu_toolchains/arm/download.html
安裝好了之后,將 CodeSourcery編譯器的bin目錄 (我的是D:\Program Files\CodeSourcery\Sourcery G++ Lite\bin)加入你的PATH環(huán)境變量中,就可以開始你的Android Native C開發(fā)之旅了,寫好一個簡單的C程序:
#include <stdlib.h>
int main(int argc, char** argv) {
printf("hello android!\nI'm %s!\nI like android very much!!!\n", "Martin Foo");
return 0;
}
另存成hello.c,進入命令行模式,確保交叉編譯器的bin目錄,及Android SDK的tools目錄在你的系統(tǒng)環(huán)境變量的path里面,用如下命令編譯:
arm-none-linux-gnueabi-gcc -static hello.c -o hello
注意,一定要加上static參數(shù),否則編譯好的可能會在Android上不能運行。
啟動Android模擬器,用如下命令將文件push到Android模擬器上:
adb shell mkdir /dev/sample
adb push hello /dev/sample/hello
adb shell chmod 777 /dev/sample/hello
先創(chuàng)建 /dev/sample目錄,再將編譯好的hello上傳上去,最后將hello改成可執(zhí)行的。
再進入命令行模式,進入Android的shell環(huán)境:
adb shell
#cd /dev/sample
#./hello
進入 /dev/sample目錄,執(zhí)行hello,運行結(jié)果如下圖:
歡迎光臨 Chinaunix (http://www.72891.cn/) | Powered by Discuz! X3.2 |