从网上可以找到一些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 $@ $(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

http://www.linuxidc.com/Linux/2011-10/45968.htm

更多相关文章

  1. Android开发之如何获取Android手机屏幕的大小(分辨率、密度)
  2. 构建 Android 手机 RSS 阅读器
  3. Android手机定位应用小解
  4. Android源码下载——用git clone实现单个目录下载
  5. android在线更新的SDK的脚本,
  6. Android音乐播放器(一):搜索手机存储的音乐
  7. 手机的CPU架构简述及SO兼容(Android/iOS)及手机硬件等,手机IMEI
  8. Android热补丁技术—dexposed原理简析(手机淘宝采用方案)
  9. Android高级应用2----ContentProvider(访问手机短信和通讯录数据)

随机推荐

  1. Android(安卓)UI开发第十二篇——动画效
  2. Android将胜过Windows Mobile五大原因
  3. Android之NDK开发详解
  4. 在电脑上pc端运行android安卓模拟器图...
  5. android之调用webservice 实现图片上传下
  6. Android(安卓)消息推送
  7. android eclipse 真机调试
  8. 【摘录】Google Android操作系统内核编译
  9. Android漫游记(1)---内存映射镜像(memory
  10. Android构建面试知识