Tuesday, January 17, 2012

cross-compiling binaries for Android device on Windows 7/Ubuntu

C language is most commonly used basic programming language. Here is a method to run a C program on an Android device with ARM processor. We cross compile the program on windows machine and generate binary files for ARM. Cross compiling is a method of compiling executable for other platform other than the one in which compiler is run. Here we are creating an executable for ARM device (running Android) platform by compiling on windows. The same method should work on LINUX platform too.

1. Download and CodeSourcery's toolchain installer for IA32 Windows host from https://sourcery.mentor.com/sgpp/lite/arm/portal/release2029 .

2. Install the setup.

3. The toolchain provides the cross compiler arm-none-linux-gnueabi-gcc. Add directory path in default path variables.

4. write a sample program as follows

#include<stdio.h>
int main(){
     printf("\n Hello World! \n");
     return 0;
}
save the program as hello.c

5. compile hello.c as follows from the cmd/terminal:
$ arm-none-linux-gnueabi-gcc -o hello -static hello.c
6. Copy the binary to phone using adb tool or just copy it to phone memory.
$ adb push hello /data
7. There are two methods to run the program
i) Running on linux shell using adb
- open linux shell of the Android device using
$ adb shell
$ cd /data
$ ./hello

//output must be:

 Hello World!
ii) running it on virtual terminal on the Android device.
- download a virtual terminal emulator from android market (https://github.com/jackpal/Android-Terminal-Emulator/wiki)

-open the virtual terminal and run the program.
$ cd /data
$ ./hello

output displayed on terminal:
Hello world!


Its interesting to see the the C programs running on the linux shell. :) :D

No comments:

Post a Comment