Android原生(Native)C開發之一:環境搭建篇

來源:互聯網
上載者:User

 原文:http://blog.sina.com.cn/s/blog_4a0a39c30100auh9.html

 

Android是基於Linux的作業系統,處理器是ARM的,所以要在Linux或Windows等x86系統上編譯Android能啟動並執行程式,你需要一個交叉編譯器。

在Linux下面,你可以自已編譯一個交叉編譯環境,但Windows下面,就比較複雜(也可以在cygwin中編譯一個),但你可以選擇下載一個現成的交叉編譯環境:

http://www.codesourcery.com/gnu_toolchains/arm/download.html

Windows:

http://www.codesourcery.com/gnu_toolchains/arm/portal/package3400/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.exe

Linux:

http://www.codesourcery.com/gnu_toolchains/arm/portal/package3399/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.bin

 

安裝好了之後,將 CodeSourcery編譯器的bin目錄 (我的是D:/Program
Files/CodeSourcery/Sourcery G++ Lite/bin)
加入你的PATH環境變數中,就可以開始你的Android Native C開發之旅了,寫好一個簡單的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目錄在你的系統內容變數的path裡面,用如下命令編譯:

arm-none-linux-gnueabi-gcc -static hello.c -o hello

注意,一定要加上static參數,否則編譯好的可能會在Android上不能運行。

 

啟動Android模擬器,用如下命令將檔案push到Android模擬器上:

adb shell mkdir /dev/sample
adb push hello /dev/sample/hello

adb shell chmod 777 /dev/sample/hello

 

先建立 /dev/sample目錄,再將編譯好的hello上傳上去,最後將hello改成可執行檔。

再進入命令列模式,進入Android的shell環境:

adb shell

#cd /dev/sample

#./hello

進入 /dev/sample目錄,執行hello,運行結果如:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.