編譯在android 平台上跑的C應用程式

來源:互聯網
上載者:User

標籤:

Android 用的是 Bionic C, 而不是通常的glibc,因此簡單使用交叉工具鏈並不能夠編譯出適合運行在android 裝置上的 C/C++ 程式。

交叉工具鏈可以很輕鬆在 Android 源碼 prebuilts 目錄下找到,但是直接編譯會提示找不到各種標準庫的標頭檔,需要指定 Bionic C庫 的 sysroot.

像 Tiny 6410 開發板帶的Android toolchain 因為整合了sysroot,因此可以直接用於編譯C/C++ 代碼。

本文講述如何為任何一個Android 裝置(ARM v7a )使用 android 提供的NDK 工具編譯代碼(busybox)。

 

1) 下載 Android NDK 交叉工具鏈及 sysroot

http://developer.android.com/tools/sdk/ndk/index.html#Downloads

解壓之後,在下面的路徑中可以找到交叉工具鏈

android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin

arm-linux-androideabi-addr2line*  arm-linux-androideabi-elfedit*  arm-linux-androideabi-gcc-ranlib*  arm-linux-androideabi-ld.gold*  arm-linux-androideabi-readelf*
arm-linux-androideabi-ar*         arm-linux-androideabi-g++*      arm-linux-androideabi-gcov*        arm-linux-androideabi-ld.mcld*  arm-linux-androideabi-run*
arm-linux-androideabi-as*         arm-linux-androideabi-gcc*      arm-linux-androideabi-gdb*         arm-linux-androideabi-nm*       arm-linux-androideabi-size*
arm-linux-androideabi-c++*        arm-linux-androideabi-gcc-4.8*  arm-linux-androideabi-gprof*       arm-linux-androideabi-objcopy*  arm-linux-androideabi-strings*
arm-linux-androideabi-c++filt*    arm-linux-androideabi-gcc-ar*   arm-linux-androideabi-ld*          arm-linux-androideabi-objdump*  arm-linux-androideabi-strip*
arm-linux-androideabi-cpp*        arm-linux-androideabi-gcc-nm*   arm-linux-androideabi-ld.bfd*      arm-linux-androideabi-ranlib*

最好將這個路徑加入到環境變數。


android-ndk-r9/platforms/android-18/arch-arm 這個路徑下面放的是 Android API18 的sysroot.

通過http://source.android.com/source/build-numbers.html#platform-code-names-versions-api-levels-and-ndk-releases 擷取API 版本和android 發行版本的對應關係。

 

2)  編譯簡單的 hello world

$ cat hello.c 
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    printf("Hello world ! \n");
    return 0;
}

$ arm-linux-androideabi-gcc -o hello --sysroot=/home/xxx/android-ndk-r9/platforms/android-18/arch-arm/ hello.c

 

3). 移植 busybox

$ git clone git://busybox.net/busybox.git

busybox$ cp configs/android_ndk_defconfig .config

$ make menuconfig 

修改 sysroot config 到自己的路徑。

$ make -j 8

$ make install

預設安裝到 當前路徑下的 _install/

 

$ adb push _install /data/busybox/

 

參考:

https://github.com/tias/android-busybox-ndk

That‘s all ! Enjoy your busybox on your own Android Now !

編譯在android 平台上跑的C應用程式

聯繫我們

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