使用Android NDK中的獨立toolchain來開發C/C++程式
從網上可以找到一些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
http://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
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("Hello Andriod.\n");
return 0;
}
Makefile
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 [email protected] $(OBJS)
%.o:%.c
$(CC) -c $(CFLAGS) $< -o [email protected]
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許可權
參考:
http://groups.google.com/group/android-ndk/browse_thread/thread/7506768ccf52cea2?pli=1