So I Made My iPhone Say Hello, World…

…with the open source toolchain. It was a real pain until I decided to focus on the easy way. Then it wasn’t hard at all.

For reference, the hard way is on iphone-dev. I spent most of a frustrating, unsuccessful day with that page and its thousands of comments, especially extracting the SDK from the 1GB XCode binaries - you want version 2.5, not a newer version, and use PowerISO to extract it. For what it’s worth, there looks to be an easier way for Cygwin on Windows.

The Easy Way

But the really easy way is to install the development tools on your iPhone.

This requires:

  • a “jailbroken” phone - so you can install open software on it.

    I used Windows QuickPwn and it was easy. Works with the 2.1 firmware, too…

  • a Wi-Fi connection - and get the IP address of your iPhone:
    • open the iPhone Settings application
    • tap Wi-Fi
    • tap the (>) button to the right of your active connection
    • note the IP Address - probably something like 192.168.1.101, which I use below
  • an SSH client of your choice - Linux and Mac come with ssh clients.

    Windows users can download PuTTY or use the Cygwin Unix layer on top of Windows. Developers running Windows may also want to grab WinSCP for its “Keep Remote Directory Up To Date” feature (more on that in another post).

After the Jailbreak

After your phone is jailbroken, open the Cydia application. It is like the AppStore, but for open software.

Tap “Featured Applications”, then flick to the bottom, and tap OpenSSH, then tap Install, tap Confirm.

When the installation completes, go back to Cydia, tap Search in the bottom-right, and type toolchain. Tap iPhone 2.0 Toolchain, then Install then Confirm. This is a pretty big iPhone application - over 40MB expanded! So, while your iPhone is working on that, let’s go back to the desktop machine.

You should now be able to use ssh (or PuTTY…) from your desktop computer to connect to your iphone, using root as the user, your iPhone’s IP address for the host, and alpine for the password. If you’re using a graphical SSH client like PuTTY, follow its prompts. On Mac/Linux/Cygwin, open a terminal, and type:

      ssh root@192.168.1.101
      root@192.168.1.101's password:              [type alpine]

The first time you connect, it may take about 30 seconds, then you should then see a Unix command-line prompt from the iPhone, and it will end with a “#” - which means you are the “root” or administrative user. You’re the boss - congratulations!

First thing you should do as boss is make sure nobody else can be boss - change your password! (No, really, change it now - here’s how…)

    passwd
    Changing password for root.
    New password:               
    Retype new password:        

Don’t forget this password. :-D If you do, you’ll probably have to jailbreak the phone again to reset it.

After the Toolchain is Installed

OK, so you can ssh into your iPhone, and you’ve installed the toolchain (that installation has completed on your phone, right?).

So, on your desktop with the ssh window into your iPhone, type:

    echo 'main() { printf("Hello, iPhone!\n"); }' > hello.c
    gcc hello.c -o hello
    ldid -S hello
    ./hello

And your phone should say:

    Hello, iPhone!

When I first did this, it was late at night, and I forgot the ldid -S hello, so instead of “Hello, World”, I got:

    # ./hello
    Killed

The iPhone will only run “signed” code, and since ./hello was not signed, the iPhone killed it. The “ldid -S PROGRAM” signs the program for you. Turns out the iPhone doesn’t care what signature is on the file, just that it is marked with a signature…

So there you go: a usable toolchain on your iPhone, waiting for you to write the next great iPhone program. For real development, you’ll want to use your desktop development environment and something like sshfs, rsync, or WinSCP to copy the files to the iPhone as you edit them, but more on that later… Updated: More is now available for Windows/WinSCP and Linux/sshfs

34 Comments »

  1. Matze Said,

    September 30, 2008 @ 11:13 pm

    Thanks for your little tutorial

  2. So I Found Ways to Develop for iPhone Said,

    October 7, 2008 @ 9:17 pm

    [...] you can even develop ON the iPhone [...]

  3. Ric01 Said,

    October 20, 2008 @ 10:26 pm

    Hey!!
    Excellent work!
    Nicely done!, im trying to install the toolchain the iphone-dev way on ubuntu 8.04 but failed on oddctools installation, your solution works nicely.
    Thanks again.
    Ric01, Caracas, Venezuela

  4. tiYakusa Said,

    October 24, 2008 @ 2:27 am

    Hi,

    I tried this, but when gcc compiles, it complains about implicit declaratio of built-in ‘printf’.

    Anyway it creates the ‘hello’ file, but when I try to run it, nothing appears…

    Any idea?

    Maybe I have to add an #include at beginning.

    Bye

  5. kb Said,

    October 24, 2008 @ 9:02 am

    Rico1, glad I could help.

    tiYakusa, the warning is expected, and yes, you can get around it by doing the #include as you expected:

    #include <stdio.h>
    main() { printf(”Hello, iPhone!\n”); }

    However, it shouldn’t be necessary.

    Here’s an exact transcript, copied from my ssh window and the angle brackets changed to &lt;/&gt;:
    $ cat hello.c
    #include <stdio.h>
    main() { printf(”Hello, iPhone!\n”); }
    $ gcc hello.c -o hello
    $ ldid -S hello
    $ ./hello
    Hello, iPhone!
    $

    Can you copy and paste the same for your failure?

    kb

  6. Mononofu Said,

    November 5, 2008 @ 1:13 pm

    First of all, thanks for this how-to! It really helped me out, since I don’t have a Mac (yet).

    But you said you would create a follow-up tutorial on how to synch the files on your phone everytime you change them, which would be quite cool. Sadly, there is no such article yet - do you still have plans on doing something like that?

  7. Illarion Said,

    November 5, 2008 @ 9:08 pm

    Actually, quite frankly, the commentary is more interesting messages themselves. (Not to insult the author, of course:))

  8. kb Said,

    November 5, 2008 @ 11:34 pm

    Mononofu: I added another post explaining how to do this with

    WinSCP: http://soi.kd6.us/2008/11/05/so-i-used-winscp-to-keep-a-remote-directory-up-to-date/
    sshfs: http://soi.kd6.us/2008/11/05/so-i-used-sshfs-to-mount-a-remote-filesystem/

    Illarion: No offense taken, I think. :-)

  9. Darius Said,

    November 6, 2008 @ 8:26 pm

    It has long been looking for this information, Thank you for your work.

  10. markdude Said,

    November 12, 2008 @ 6:14 am

    ok great. This stuff is working!
    but now, how can I create an icon for my program on the Springboard?
    Thanks,
    markdude

  11. markdude Said,

    November 12, 2008 @ 5:02 pm

    This article was my first step into the brave world of toolchain. I found another article that gives “hello world” example with Springboard update
    http://antirez.com/page/iphone-gcc-guide.html

    Illarion, thank you

  12. Kyle Said,

    December 3, 2008 @ 1:39 pm

    i’m still trying to understand toolchain iphone. thank you for this tutorial. i didn’t know i can code directly in C. now I am going to try C++. however instructions tell no need to include stdio.h, but when i try to ‘find / -name stdio.h’ on my phone, there’s no such file. how can gcc simply compile and make it work???? thanks

  13. Lars Ole Said,

    December 14, 2008 @ 1:54 pm

    Thank you for the great tutorial. The toolchain installed a treat on 2.2 firmware (ipod touch). Command line ‘hello world’ compiles and runs without a hitch.

    Anything requiring UIApplicationMain to be called poses a problem, though. When building/running as user ‘mobile’, I just get the output ‘Killed’ any time this function is invoked. When building/running as root, the program hangs without producing any output.

    I am of course using ldid as described.

    I hope somebody has a clue on what I’m doing wrong.

  14. dante Said,

    January 19, 2009 @ 12:01 am

    Thanks for the tutorial! I too gave up on trying to use the other cross-compiling toolchains.

    Some answers to the other comments that found through my own trial and error:

    - ldid -S works fine for me. Make you have it installed first though. Either run on the command line, “apt-get install ldid” (as root), or install the “Link Identity Editor” package on Cydia.

    - To get the springboard icon to show up (I’m on 2.2 firmware), first follow the instructions from http://antirez.com/page/iphone-gcc-guide.html to have the right files in your .app directory (Info.plist, icon.png, Pkg.info). Then install the “UIKit Tools” package on Cydia. Now you can run “uicache” (as mobile) in the command line. Ignore the stuff about killing springboard, it’s an overkill.

    - @Kyle: it doesn’t sound like you have the toolchain package installed as per instructions.

    Finally, a caveat with this technique is the use of the iPhone 2.0 toolchain package on 2.2 firmware. This package is fundamentally out of date if you want to develop new applications since it’s specifically for building iPhone 1.1.X applications on 2.0 firmware. What this means is that if you’re on 2.2, some things just won’t compile. Thus far, I’ve found that the UIKit/UIViewController.h is unusable. So this method is only good with you plan on doing toy apps, IMHO.

  15. dante Said,

    January 19, 2009 @ 1:16 am

    Update: I’ve managed to use UIKit/UIViewController.h by downloading the official iPhone SDK 2.2 and then extracting the headers using the latest stable version of 7-zip (7.64). Read this page for info on how to do this:
    http://theiphonewiki.com/wiki/index.php?title=Toolchain_2.0

  16. dante Said,

    January 19, 2009 @ 1:18 am

    PS. Extracting the SDK headers on Windows was relatively easy using 7-zip. Just open the .dmg file using 7-zip and keep double-clicking.

  17. rich Said,

    January 20, 2009 @ 6:07 am

    hi dante, I have the same problem as Kyle, whenever in include
    I get error: stdio.h:
    No such file or directory

    if I don’t include it, I get
    warning: incompatible implicit declaration of built-in function ‘printf’
    but nonetheless, when I ran ./hello it seems to work fine.
    I wonder why that is, I installed the toolchain directly from Cydia, in your reply to

    - @Kyle: it doesn’t sound like you have the toolchain package installed as per instructions.

    What do you mean instruction? what have I missed?

    thanks.

  18. Befused Said,

    January 21, 2009 @ 8:15 pm

    Can apps developed by this gcc toolchain be sellable on official Apple App Store?

    Yes, I am cheap. I want to make lots of money, but I don’t want to buy a Mac.

    Thanks

  19. kb Said,

    January 22, 2009 @ 9:54 am

    At this point, the only way to build an application for the AppStore is via the official SDK (xkcode, etc.). This requires OS X.

    However, you could do all your development with the free toolchain, then do the final build on the official SDK.

    You could even borrow the Mac to do the final build, but you will have to have (or know somebody who has) the $99 membership to the iPhone Developer program.

    kb

  20. ChaZg33k Said,

    March 5, 2009 @ 7:14 pm

    Why would you want to use the App Store when you could submit to Cydia?

  21. So I Considered AppStore vs. Cydia Said,

    March 6, 2009 @ 10:02 am

    [...] So I Considered AppStore vs. Cydia From ChaZg33k on So I Made My iPhone Say Hello, World… [...]

  22. kb Said,

    March 6, 2009 @ 10:03 am

    @ChaZg33k - took this as a serious question, and responded: http://soi.kd6.us/2009/03/06/appstore-vs-cydia/

  23. bashhacker.com » Blog Archive » you gotta ldid -S your iPhone console apps after you g++ them Said,

    March 10, 2009 @ 3:49 am

    [...] so that they will run… http://soi.kd6.us/2008/09/27/so-i-made-my-iphone-say-hello-world/ [...]

  24. xilvar Said,

    June 3, 2009 @ 11:48 pm

    say, goodl post, but you should definitely remove the bit about using passwd to change your root password. if you do it that way it actually messes up the password tables which you may not notice until the next time you reboot. the passwd program on the iphone creates the hashes wrong… look around for the proper way to do it…

  25. kb Said,

    June 4, 2009 @ 6:44 am

    Interesting, xilvar, but I’ve never had a problem with using passwd to change my iphone root password.

    It looks like this was a historical problem (see http://www.saurik.com/id/1) but following the jailbreak method I used (http://soi.kd6.us/2008/09/25/so-i-got-a-new-phone/) I never had a problem.

    kb

  26. teabaggs Said,

    June 8, 2009 @ 10:34 pm

    when i installed the headers they went to /var/include but…
    >gcc -v hello.c
    #include “…” search starts here:
    #include search starts here:
    /usr/lib/gcc/arm-apple-darwin8/4.2.1/include
    /usr/include
    /System/Library/Frameworks (framework directory)
    /Library/Frameworks (framework directory)
    End of search list.
    GNU C version 4.2.1 (Based on Apple Inc. build 5555) (arm-apple-darwin8)
    compiled by GNU C version 4.2.1 (Based on Apple Inc. build 5555).
    GGC heuristics: –param ggc-min-expand=37 –param ggc-min-heapsize=14848
    Compiler executable checksum: e0ef93d2f2f57f4a826ac880faa82584
    test.c:1:19: error: stdio.h: No such file or directory
    test.c: In function ‘main’:
    test.c:8: warning: incompatible implicit declaration of built-in function ‘printf’

    its not in the default Include paths so try

    gcc hello.c -I/var/include

    worked for me

  27. romonoeroetoko Said,

    July 8, 2009 @ 9:16 am

    Hm that sounds good but I would like to know more details.

  28. romonoeroetoko Said,

    July 15, 2009 @ 10:47 am

    Your news is a cool stuff man, keep it going.

  29. AG Said,

    August 18, 2009 @ 11:35 am

    AGs-Touch:/tmp root# cat temp.c
    #include
    main()
    {
    printf(”hello, Ipod touch”);
    return 0;
    }
    AGs-Touch:/tmp root# chmod 777 temp.c
    AGs-Touch:/tmp root# gcc temp.c -o temp
    AGs-Touch:/tmp root# chmod 777 temp
    AGs-Touch:/tmp root# ldid -S temp
    AGs-Touch:/tmp root# ./temp
    Killed
    AGs-Touch:/tmp root#

  30. AG Said,

    August 18, 2009 @ 11:37 am

    my bad, hit the submit button before completing it

    though i signed the executable, its not getting executed…

    any reasons???

    am using jailbroken Ipod touch on FW:3.0

  31. adamoerikom Said,

    September 19, 2009 @ 2:24 pm

    Stunning blog and good article. High 5 for u man !

  32. ikea_repairman Said,

    October 24, 2009 @ 8:19 pm

    Any ideas?

    $ gcc hello.c -o hello
    hello.c: In function ‘main’:
    hello.c:2: warning: incompatible implicit declaration of built-in function ‘printf’
    ld: library not found for -lgcc_s.10.5
    collect2: ld returned 1 exit status

    $ ls -al /usr/lib/libgcc_s.10.*
    lrwxr-xr-x 1 root admin 16 Oct 24 22:30 /usr/lib/libgcc_s.10.4.dylib -> libgcc_s.1.dylib
    lrwxr-xr-x 1 root admin 16 Oct 24 22:30 /usr/lib/libgcc_s.10.5.dylib -> libgcc_s.1.dylib

    $ ls -al /usr/lib/libgcc_s.1.dylib
    ls: cannot access /usr/lib/libgcc_s.1.dylib: No such file or directory

  33. Daniel Said,

    November 22, 2009 @ 10:25 am

    Thanks! I first tried another tutorial (it wasn’t very helpful). It never told me to sign the output with ldid. Thanks very much!!!!

  34. OnekaminnaGed Said,

    August 7, 2010 @ 5:34 am

    видео порно смотреть

Leave a Comment