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

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.


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

  2. How to run a c program using Cygwin, NDK for android.
    Please reply in

  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 :(

  4. Hi,

    I am also facing same error when compiling like

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

    Please advice on this..


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

  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

    It worked for me...

  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?

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

    best android apps

    best iphone apps

  9. 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?

  10. Please refer to this link

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

  12. she said

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

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

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

  15. Great post.Thanks for one marvelous posting! I enjoyed reading it;The information was very useful. Please kindly visit our site to get more information about.
    app development company in bhopal
    Top It companies in Bhopal
    Companies in MPNagar Bhopal