2010年9月11日星期六

Build ARM code on Linux/eclipse


Know how to setup ARM toolchain;

(http://www.icdev.com.cn/?734/viewspace-653.html).

安装交叉编译环境

1. 安装标准的C开发环境,由于Ubuntu安装默认是不安装的,所以需要先安装一下:

sudo apt-get install gcc g++ libgcc1 libg++ make gdb

2.安装交叉编译器

从ftp://ftp.arm.linux.org.uk/pub/armlinux/toolchain/下载交叉编译器cross-3.2.tar.bz2,然后解压sudo tar jxvf /home/engelbert/cross-3.2.tar.bz2(/home/engelbert/为你存放cross-3.2.tar.bz2的路径),然后将解压后的文件都转移到/usr/local/下,

sudo mv /home/engelbert/usr/local/arm /usr/local

3.把交叉编译器的路径加入到PATH

sudo vi /etc/bash.bashrc

在后面加入如下代码:

if [ -d /usr/local/arm ] ; then

PATH=/usr/local/arm/bin:"${PATH}"

fi

4.使新的环境变量生效

# source /etc/profile

5.检查是否将路径加入PATH的方法:

echo $PATH

如果显示的内容中有/usr/local/arm/bin,说明已经将交叉编译器的路径加入PATH

自此,交叉编译环境安装完成。

下面我们就来测试一个简单的例子。

/*Hello.c*/

#include

3. main()

{

printf(hello ubuntu!\n);

}

程序输好以后确认无误,保存。进入程序文件所在目录

arm-linux-gcc hello.c –o hello(-o 可以理解为“目标为生成”)

arm-linux-gcc是第一次出现,有人可能会问这个哪里来的,不妨打开刚才安装的交叉编译工具目录/usr/local/arm-linux/arm-linux/bin/可以发现里面有一个arm-linux-gcc文件,这个就是针对arm的CPU的gcc编译器了。以后用其它编译工具链式也可以通过这种方法看看其编译器是什么了。编译好了以后就可以下载到目标机进行测试了。当然也可以先在PC机上测试正误。用gcc hello.c –o hello就可以生成PC机上程序了,在运行./hello 就可以发现终端显示hello!字样。用arm-linux-gcc编译的程序在PC机上是不能运行的,运行后给出错误报告:无法执行二进制文件。说明经过交叉编译环境编译出的文件是硬件可执行的二进制代码文件。







Put ARM toolchain in Eclipse

http://www.cnblogs.com/xubing/archive/2010/06/02/1749798.html

http://www.cnblogs.com/xubing/archive/2010/06/07/1752405.html

http://www.cnblogs.com/xubing/archive/2010/06/07/1753257.html

默认情况下,arm-linux-gcc会被解压到/usr/local/arm/4.3.2/目录中,其中,可执行文件存储在/usr/local/arm/4.3.2/bin中。解压成功后,我们需要修改环境变量,使得Linux可以找到arm-linux-gcc。使用gedit打开/etc/environment,在原有语句后面加上/usr/local/arm/4.3.2/bin。

我的/etc/environment是这样的:



PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/arm/4.3.2/bin"

注意:Linux中有3个级别的环境变量。这个/etc/environment是其中级别较高的一个。



如果我们修改私有的环境变量,Eclipse会提示无法找到arm-linux-gcc。必须修改/etc/environment,才能让Eclipse找到arm-linux-gcc。我猜测这是由于Eclipse运行于JAVA虚拟机上造成的。

至此,arm-linux-gcc就已经安装完成了,注销一下,再重新登录。打开一个终端,在里面输入arm-linux-gcc -v,如果返回了版本信息的话,就说明arm-linux-gcc安装成功了!







Re-use Android ARM toolchain both command line

http://www.pocketmagic.net/?p=682

Android uses a simplified version of libc, called bionic. We need to compile using Android's prebuilt cross-compiler arm-eabi-gcc, and use the bionic library on the phone.

The easy way to do this is to use the agcc perl wrapper. You can download the original file here, and modify

my $TOOLCHAIN = "$DROID/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1";

to

my $TOOLCHAIN = "$DROID/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1";



Or download this updated version directly.

Copy agcc to your home directory, and chmod it:



chmod +x agcc



then set the PATH to the bionic libs and the agcc location:



$ PATH=$PATH:~/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/bin:~/:~/mydroid/

Now you can compile the test.c file:



agcc test.c -o test

Take the resulting test binary and upload it to your android using:



adb push test /data/local/test



Then run it:



adb shell

chmod 775 test

./test