Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

在 Android 上編譯並執行原生 C 程式:NDK 交叉工具鏈實作教學

編輯:關於 Android 開發

讓 Android 直接跑 C 程式

對做上層 App 的工程師來說,「在 Android 上跑 C 程式」聽起來有點陌生,畢竟應用層開發繞不開 Java(以及後來的 Kotlin)。但對底層驅動與系統層的人來說這是日常:Android 是 Linux 分支,底層世界說的是 C 與 C++。

驗證某些機制時,我們確實需要一支直接跑在 Android 終端下的原生程式。前提有兩個:一套 Android 交叉編譯器,以及裝置的 root 權限。

一、把交叉工具鏈從 NDK 裡拆出來

NDK 為 native 開發做了很多事,但我們只需要其中的交叉編譯工具。以 64 位元的 Ubuntu 14.04 為例,下載對應版本的 NDK:

ndk$ chmod a+x android-ndk-r10e-linux-x86_64.bin
ndk$ ./android-ndk-r10e-linux-x86_64.bin

接著用 NDK 內附的腳本產生獨立的工具鏈:

$ ./build/tools/make-standalone-toolchain.sh \
    --platform=android-19 \
    --toolchain=arm-linux-androideabi-4.9

腳本會把預編譯的執行檔、sysroot 標頭檔與函式庫打包成 tar.bz2,解開後即可使用:

build-tools$ tar jxvf arm-linux-androideabi-4.9.tar.bz2

二、Hello, Native

寫一支最簡單的 C 程式 main_test.c:

#include <stdio.h>

int main() {
    printf("just a test, linc! hello, native!\n");
    return 0;
}

用剛剛產生的工具鏈編譯:

$ ~/bin/build-tools/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc \
    -o main_test main_test.c

推到裝置上執行:

$ adb push main_test /data/app
$ adb shell
root@hammerhead:/ # cd /data/app
root@hammerhead:/data/app # chmod 755 main_test
root@hammerhead:/data/app # ./main_test
just a test, linc! hello, native!

就跟在 Linux 上一模一樣。順帶一提,/data/local/tmp 是更常見的暫存位置,某些 SELinux 較嚴格的版本對 /data/app 的執行權限會擋下來。

三、編譯多檔案專案

把排序邏輯與測試進入點拆成兩個檔案, header 放在 shooter.h:

/* shooter.c */
#include "shooter.h"
#include <stdio.h>

void bubble_sort(int *array, int n) {
    int i, j, tmp;
    for (i = 0; i < n - 1; i++) {
        for (j = n - 1; j > i; j--) {
            if (array[j - 1] > array[j]) {
                tmp = array[j - 1];
                array[j - 1] = array[j];
                array[j] = tmp;
            }
        }
    }
}

int A(int a) {
    int array[] = {54, 12, 346, 5, 23, 67, 234, 324, 45, 98};
    bubble_sort(array, 10);
    return array[0];
}
/* shooter_tester.c */
#include <stdio.h>
#include "shooter.h"

int main() {
    int result = A(0);
    printf("A result: %d\n", result);
    return 0;
}

編譯時把兩個 .c 一起交給 gcc 即可,不必先各自編譯成 .o:

$ arm-linux-androideabi-gcc -o test shooter_tester.c shooter.c
$ adb push test /data/app/
$ adb shell
root@hammerhead:/data/app # ./test
A result: 5

四、加上隨機數:一個真實的踩雷紀錄

把原本的固定陣列改成用 rand() 產生:

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int A(int a) {
    int n = 10, i;
    int array[n];
    srand(time(NULL));
    for (i = 0; i < n; i++) {
        array[i] = rand() % 100;
    }
    bubble_sort(array, n);
    return array[0];
}

編譯時卻出現了:

error: undefined reference to `srand'
error: undefined reference to `rand'

原因很單純:rand 與 srand 的宣告在 stdlib.h 裡,只引入 time.h 是不夠的。補上標頭檔後就正常了:

root@hammerhead:/data/app # ./test
18, 95, 91, 55, 13, 37, 74, 85, 83, 66
A result: 13
root@hammerhead:/data/app # ./test
59, 100, 84, 32, 26, 46, 11, 50, 44, 83
A result: 11

結語

到這裡,你手上的 Android 裝置基本上就能當成一台 Linux 來玩了。交叉編譯的門檻比想像中低,真正的成本在於理解目標環境的檔案系統權限與 SELinux 政策。祝你折騰愉快。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved