---
title: "Android原生(Native)C开发之一：环境搭建篇"
author: "Ancool"
date: "2011-01-23T20:12:56Z"
tags: []
categories: ["编程语言"]
slug: "android-native-c"
draft: false
lastmod: "2011-01-23T20:12:56Z"
id: 891971
url: "https://ancool.iteye.com/blog/891971"
---

Android是基于Linux的操作系统，处理器是ARM的，所以要在Linux或Windows等x86系统上编译Android能运行的程序，你需要一个交叉编译器。

在Linux下面，你可以自已编译一个交叉编译环境，但Windows下面，就比较复杂（也可以在cygwin中编译一个），但你可以选择下载一个现成的交叉编译环境：

http://www.codesourcery.com/gnu\_toolchains/arm/download.html

Windows: http://www.codesourcery.com/gnu\_toolchains/arm/portal/package3400/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.exe

Linux: http://www.codesourcery.com/gnu\_toolchains/arm/portal/package3399/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.bin

安装好了之后，将 CodeSourcery编译器的bin目录 (我的是D:\\Program Files\\CodeSourcery\\Sourcery G++ Lite\\bin）加入你的PATH环境变量中，就可以开始你的Android Native C开发之旅了，写好一个简单的C程序：

#include &lt;stdlib.h&gt;

int main(int argc, char\** argv) {  
 printf("hello android!\\nI'm %s!\\nI like android very much!!!\\n", "Martin Foo");  
   
 return 0;  
}

另存成hello.c,进入命令行模式，确保交叉编译器的bin目录，及Android SDK的tools目录在你的系统环境变量的path里面，用如下命令编译：

arm-none-linux-gnueabi-gcc -static hello.c -o hello

注意，一定要加上static参数，否则编译好的可能会在Android上不能运行。

启动Android模拟器，用如下命令将文件push到Android模拟器上：

adb shell mkdir /dev/sample  
adb push hello /dev/sample/hello

adb shell chmod 777 /dev/sample/hello

先创建 /dev/sample目录，再将编译好的hello上传上去，最后将hello改成可执行的。

再进入命令行模式，进入Android的shell环境：

adb shell

#cd /dev/sample

#./hello

进入 /dev/sample目录，执行hello，运行结果如下图：

 

![](http://dl.iteye.com/upload/attachment/402806/0ef7b340-f9ac-347a-b87f-d12c36a4858b.jpg)