Android編譯環境— 編譯hello world模組

來源:互聯網
上載者:User

Android編譯環境--- 編譯hello world模組

Android編譯環境本身比較複雜,且不像普通的編譯環境:只有頂層目錄下才有Makefile檔案,而其他的每個component都使用統一標準的Android.mk. Android.mk檔案本身是比較簡單的,不過它並不是我們熟悉的Makefile,而是經過了Android自身編譯系統的很多處理,因此要真正理清楚其中的聯絡還比較複雜,不過這種方式的好處在於,編寫一個新的Android.mk來給Android增加一個新的Component會比較簡單。
編譯Java程式可以直接採用Eclipse的Integration Environment來完成,這裡就不重複了。我們主要針對C/C++來說明,下面通過一個小例子來說明,如何在Android 中增加一個C程式的Hello World:
1. 在$(YOUR_ANDROID)/ development 目錄下建立hello目錄,其中$(YOUR_ANDROID)指Android原始碼所在的目錄。
- # mkdir $(YOUR_ANDROID)/development/hello
2. 在$(YOUR_ANDROID)/external/hello/目錄編寫hello.c檔案,hello.c的內容當然就是經典的HelloWorld程式:
#include <stdio.h><br />int main()<br />{<br /> printf("Hello World!/n");<br /> return 0;<br />}
 
3. 在$(YOUR_ANDROID)/external/hello/目錄編寫Android.mk檔案。這是Android Makefile的標準命名,不要更改。Android.mk檔案的格式和內容可以參考其他已有的Android.mk檔案的寫法,針對helloworld程式的Android.mk檔案內容如下:
LOCAL_PATH:= $(call my-dir)<br />include $(CLEAR_VARS)<br />LOCAL_SRC_FILES:=hello.c<br /> LOCAL_MODULE := helloworld<br />include $(BUILD_EXECUTABLE)
注意上面LOCAL_SRC_FILES用來指定源檔案;,LOCAL_MODULE指定要編譯的模組的名字,下一步驟編譯時間就要用到;include $(BUILD_EXECUTABLE)表示要編譯成一個可執行檔,如果想編譯成動態庫則可用BUILD_SHARED_LIBRARY,這些可以在$(YOUR_ANDROID)/build/core/config.mk查到。
4. 回到Android原始碼頂層目錄進行編譯:
# cd $(YOUR_ANDROID) && make helloworld
注意make helloworld中的目標名helloworld就是上面Android.mk檔案中由LOCAL_MODULE指定的模組名。編譯結果如下:
target thumb C: helloworld <= development/hello/hello.c
target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)
target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)
Install: out/target/product/generic/system/bin/helloworld
 
5.如上面的編譯結果所示,編譯後的可執行檔存放在out/target/product/generic/system/bin/helloworld,通過”adb push”將它傳送到模擬器上,再通過”adb shell”登入到模擬器終端,就可以執行了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.