使用Android NDK中的獨立toolchain來開發C/C++程序 – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

從網上可以找到一些ARM toolchain,但是由於Android系統使用的不是glibc而是Bionic libc。因此隻能使用靜態編譯程序。

 

其實Android的NDK自帶瞭toolchain,但是不能直接使用NDK目錄內的toolchain,否則會出現找不到crtbegin_dynamic.o文件。

 

即使用-L指定目錄或者直接放到gcc命令行也還是提示該文件找不到。(參考最後附上的鏈接)。

 

其實Android NDK提供瞭腳本來剝離出單獨的toolchain,腳本的名字叫make-standalone-toolchain.sh

 

 

 

1. 下載Android NDK

 

https://developer.android.com/sdk/ndk/index.html

 

我用的是android-ndk-r6b

 

 

 

2. 提取toolchain

 

 

 

可以參考文檔docs/STANDALONE-TOOLCHAIN.html

 

在linux系統中解壓NDK,假設解壓到/opt;

 

cd /opt/android-ndk-r6b/

 

build/tools/make-standalone-toolchain.sh –platform=android-8

 

 

expr: warning: unportable BRE: `^\\([^\\-].*\\)$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

expr: warning: unportable BRE: `^\\(–[^=]*\\)=.*$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

expr: warning: unportable BRE: `^–[^=]*=\\(.*\\)$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

Auto-config: –toolchain=arm-linux-androideabi-4.4.3

Copying prebuilt binaries…

Copying sysroot headers and libraries…

Copying libstdc++ headers and libraries…

expr: warning: unportable BRE: `^\\([^\\-].*\\)$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

expr: warning: unportable BRE: `^\\(–[^=]*\\)=.*$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

expr: warning: unportable BRE: `^\\(–.*\\)$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

expr: warning: unportable BRE: `^\\([^\\-].*\\)$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

expr: warning: unportable BRE: `^\\([^\\-].*\\)$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

expr: warning: unportable BRE: `^\\([^\\-].*\\)$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

expr: warning: unportable BRE: `^\\(–[^=]*\\)=.*$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

expr: warning: unportable BRE: `^–[^=]*=\\(.*\\)$': using `^' as the first character

of the basic regular expression is not portable; it is being ignored

Creating package file: /tmp/ndk-hansel/arm-linux-androideabi-4.4.3.tar.bz2

Cleaning up…

Done.

 

 

 

有一些警告沒有關系,最終得到的是一個壓縮包/tmp/ndk-hansel/arm-linux-androideabi-4.4.3.tar.bz2

 

註意:這個是我的Linux機器上的目錄。

 

 

 

3. 解壓單獨的toolchain

 

可以解壓到任意目錄,這裡假設為/opt/

 

 

 

4.寫個hello world 程序試試

 

hello.c

 

 

view plain

#include <stdlib.h> 

#include <stdio.h> 

 

 

int main(int argc, char* argv[]) 

  printf("Hello Andriod.\n"); 

  return 0; 

 

Makefile

 

 

 

view plain

export PATH:=/opt/android/arm-linux-androideabi-4.4.3/bin:${PATH} 

CROSS_COMPILE=arm-linux-androideabi- 

CC=$(CROSS_COMPILE)gcc 

LD=$(CROSS_COMPILE)ld 

 

PROG=hello 

OBJS=hello.o 

 

$(PROG):$(OBJS) 

    $(CC) $(LDFLAGS) -o $@ $(OBJS)  

 

%.o:%.c 

    $(CC) -c $(CFLAGS) $< -o $@ 

 

clean: 

    rm -rf *.o $(PROG) 

 

編譯:

   make

 

可以得到hello可執行文件。

 

$ file hello

hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

 

可見是動態鏈接的。

 

 

上傳到手機裡執行。如果用數據線連接瞭手機,而且安裝瞭Android SDK,可以使用adb命令。

 

adb push hello /system/sbin/hello

 

adb shell chmod 777 /system/sbin/hello

adb shell /system/sbin/hello

如果沒有SDK,可以在手機裡安裝一個QuickSSHd程序,通過WiFi用Putty之類的軟件連接到手機終端。通過SFTP來傳送文件。

 

 

# ./hello

Hello Andriod.

 

 

註意: 手機需要有root權限

參考:

 

https://groups.google.com/group/android-ndk/browse_thread/thread/7506768ccf52cea2?pli=1

摘自 Hansel的專欄

發佈留言