当前位置:编程学习 > C/C++ >>

使用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

 

 

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权限

参考:

 

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

摘自 Hansel的专栏

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,