Tips & Tricks on Android

Friday, February 20, 2009

Hello World in C on Android

If you've been following the Android world, you'd know that Android's SDK requires you to write apps in Java. Ever since I got my phone I've been dying to run a Hello World C program, but I've been unable to get the right cross compiler for it.

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).
  1. Download CodeSourcery's toolchain installer for GNU/Linux target for IA32 host
  2. Install it: sh arm-2008q3-72-arm-none-linux-gnueabi.bin
  3. 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
  4. Copy the binary to your phone and run it from an adb shell prompt: ./hello
Voila!

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.

18 comments:

  1. hii..that was interesting. check this out:

    http://hashspeaks.wordpress.com/2009/04/18/how-to-compilerun-and-package-c-programs-in-android/

    ReplyDelete
  2. How to run a c program using Cygwin, NDK for android.
    Please reply in nareshiit.sharma@gmail.com

    ReplyDelete
  3. I tried this and got this error when compiling :(

    ./sdcard/hello: 1: Syntax error: word unexpected (expecting ")")

    Ive done some searching on google but nothing has pointed me in the right direction :(

    ReplyDelete
  4. Hi,

    I am also facing same error when compiling like

    1: Syntax error: word unexpected (expecting ")")

    Please advice on this..

    NBK

    ReplyDelete
  5. First of all, just in case, compile your programs with -static like so:
    ./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

    ReplyDelete
  6. 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.

    All 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...

    ReplyDelete
  7. I compiled as described above but I get the following error when I try to execute on the phone:

    Illegal instruction (core dumped) ./hello

    What am I doing wrong?

    ReplyDelete
  8. Had an issue with also getting an "Illegal instruction". Can you post your hello.c, just to make sure I am doing something similar enough?

    ReplyDelete
  9. Please 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/

    ReplyDelete
  10. I surmise I have chosen an intelligent and mind blowing website with interesting material.
    Garry Rein

    ReplyDelete
  11. she said


    Outstanding blog you guys have preserved there, I totally valuate the effort.
    175.

    ReplyDelete
  12. 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.
    cheap football jerseys
    jerseys cheap

    ReplyDelete
  13. I touch really precise version these articles I stingy there are writers that can create moral stuff.check here

    ReplyDelete
  14. 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.
    visit disneyhubportal

    ReplyDelete
  15. 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.
    visit disneyhubportal

    ReplyDelete
  16. 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.
    visit disneyhubportal

    ReplyDelete
  17. 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.
    visit disneyhubportal

    ReplyDelete