Today I finally managed to do it using CodeSourcery's cross compiler on linux. A cross compiler is something which allows you to compile on one architecture (the host) and run the executable binary on another (the target). In this case, the host is the linux machine where we'll install the toolchain (linux intel x86) and android phone is the host (arm).
- Download CodeSourcery's toolchain installer for GNU/Linux target for IA32 host
- Install it: sh arm-2008q3-72-arm-none-linux-gnueabi.bin
- The toolchain provides the cross compiler arm-none-linux-gnueabi-gcc. You need to put it's directory in your $PATH. Once you have the toolchain, you can easily compile your hello world program: arm-none-linux-gnueabi-gcc -o hello -static hello.c
- Copy the binary to your phone and run it from an adb shell prompt: ./hello
Points to note:
- The above binary is static, that is it does not use the phone's libc libraries. Android ships with its own trimmed down version of libc, called bionic. Next I'll be trying to compile and run using bionic libc, so that I don't have to compile statically
- The above steps do not use the toolchain in the android source code. I'll try later to cross compile using that.
hii..that was interesting. check this out:
ReplyDeletehttp://hashspeaks.wordpress.com/2009/04/18/how-to-compilerun-and-package-c-programs-in-android/
How to run a c program using Cygwin, NDK for android.
ReplyDeletePlease reply in nareshiit.sharma@gmail.com
I tried this and got this error when compiling :(
ReplyDelete./sdcard/hello: 1: Syntax error: word unexpected (expecting ")")
Ive done some searching on google but nothing has pointed me in the right direction :(
Hi,
ReplyDeleteI am also facing same error when compiling like
1: Syntax error: word unexpected (expecting ")")
Please advice on this..
NBK
First of all, just in case, compile your programs with -static like so:
ReplyDelete./agcc.pl -static -o hello hello.c
Then, on the android device, the file needs to be moved to a directory where it can be exected from; /system/xbin for example:
mv /mnt/sdcard/hello /system/xbin/hello
Now do the chmod wizardry:
chmod +x /system/xbin/hello
And finally execute:
/system/xbin/hello
That might be a newbie comment, but for me, a hello.c file was easy to compile under Ubuntu without the hassle of downloading tons of source files.
ReplyDeleteAll I had to do was to install an arm-compatible gcc (I used gcc-4.5-arm-linux-gnueabi) and then issuing:
gcc -b arm-linux-gnueabi -V 4.5 -static hello.c -o hello
After compiling:
adb push hello /data/local/tmp/hello
adb shell
chmod 0755 /data/local/tmp/hello
/data/local/tmp/hello
It worked for me...
I compiled as described above but I get the following error when I try to execute on the phone:
ReplyDeleteIllegal instruction (core dumped) ./hello
What am I doing wrong?
This comment has been removed by the author.
ReplyDeleteHad an issue with also getting an "Illegal instruction". Can you post your hello.c, just to make sure I am doing something similar enough?
ReplyDeletePlease refer to this link http://androidsnippets.wordpress.com/2012/08/30/how-to-run-execute-native-c-code-in-android-using-ndk-in-linux-ubuntu-operating-system/
ReplyDeleteI surmise I have chosen an intelligent and mind blowing website with interesting material.
ReplyDeleteGarry Rein
she said
ReplyDeleteOutstanding blog you guys have preserved there, I totally valuate the effort.
175.
Android OS based smartphones have been selling really well during the last few months, and some of the credit must go the large number of apps that are being created for android smartphones.
ReplyDeletecheap football jerseys
jerseys cheap
I touch really precise version these articles I stingy there are writers that can create moral stuff.check here
ReplyDeleteThe Walt Disney Company is well known as Disney by numerous . It is viewed as American enhanced worldwide that practice on broad communications and other stimulation aggregate. Disney headquarter is arranged at Walt Disney Studios complex in Burbank, California America.
ReplyDeletevisit disneyhubportal
The Walt Disney Company is well known as Disney by numerous . It is viewed as American enhanced worldwide that practice on broad communications and other stimulation aggregate. Disney headquarter is arranged at Walt Disney Studios complex in Burbank, California America.
ReplyDeletevisit disneyhubportal
The Walt Disney Company is well known as Disney by numerous . It is viewed as American enhanced worldwide that practice on broad communications and other stimulation aggregate. Disney headquarter is arranged at Walt Disney Studios complex in Burbank, California America.
ReplyDeletevisit disneyhubportal
The Walt Disney Company is well known as Disney by numerous . It is viewed as American enhanced worldwide that practice on broad communications and other stimulation aggregate. Disney headquarter is arranged at Walt Disney Studios complex in Burbank, California America.
ReplyDeletevisit disneyhubportal