Android原生(Native)C开发之一 环境搭建篇

转载:

转载: Android原生(Native)C开发之一 环境搭建篇

Android是基于Linux的操做系统,处理器是ARM的,因此要在Linux或Windows等x86系统上编译Android能运行的程序,你须要一个交叉编译器。html

在Linux下面,你能够自已编译一个交叉编译环境,但Windows下面,就比较复杂(也能够在cygwin中编译一个),但你能够选择下载一个现成的交叉编译环境:linux

http://www.codesourcery.com/gnu_toolchains/arm/download.htmlandroid

Windows: http://www.codesourcery.com/gnu_toolchains/arm/portal/package3400/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.exeshell

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程序:命令行

#includecode

int main(int argc, char** argv) {
printf(”hello android!\nI’m %s!\nI like android very much!!!\n”, “Martin Foo”);htm

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,运行结果以下图:

Android Native编译环境

Android Native编译环境

环境搭建篇

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

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,运行结果以下图:

Android Native编译环境

Android Native编译环境

相关文章
相关标签/搜索