在android的設計中,谷歌設計了一套專門為嵌入式設備使用的bionic C庫,以替換原有的GUN Libc,這個精簡的bionic庫據說只有200多K,所以如果只想使用這個精簡的C庫像在linux下一樣 開發C程序,基本是不可能的。當然如果只想讓其在shell中運行還是可以做到的。
因為編譯完的目標程序是在android下運行,就要使用交叉編譯的工具,在下面地址下載:
http://www.codesourcery.com/gnu_toolchains/arm/download.html
下載完之后,bin目錄下的arm-none-linux-gnueabi-gcc就是交叉編譯器了
#include <stdio.h> int main() { printf("nihao a\n"); printf("你好 啊\n"); return 1; }
輸入一下命令:
./arm-none-linux-gnueabi-gcc hello.c -o hello -static
-static選項在這里是必須的,否則會出現”not found”的錯誤。
然后就可以把編譯好的hello傳到手機上運行了。不過這里有個前提條件,要求android機器必須是root過的,好像簡單的z4root還不行,必須使用更徹底的root方法,關于如何root,這里就不再贅述了,可以參考相關root的帖子。
adb push hello /dev/sample/
這里要上傳的目錄必須是root用戶所有的。
然后就是運行程序,可以在adb shell里測試
adb shell
cd /dev/sample/
chmod 777 hello
./hello
或者在手機上安裝超級終端,在終端里運行
./hello