iPhone Development on Windows & Linux with open source tools

Yesterday I presented at OSCON 2009. I spoke about how to use Eclipse and the iPhone-dev team’s GCC cross-compiler to develop for iPhone on Windows & Linux. I also spoke about XMLVM, an open source project that lets you develop in Java for Android and deploy on iPhone or Palm WebOS.

You can download the slides and program examples here.

To get started with open source iPhone development, you can download the open source toolchain for Linux or for Windows/Cygwin.

Note: To use the toolchain, you need to obtain the iPhone OS headers, frameworks and libraries. You can jailbreak your device and extract the frameworks and libraries using SSH, or you can decrypt the firmware and extract the framework and libraries from the decrypted disk image. The binaries you need are /System/Library/Frameworks and /usr/lib .

You can download the headers from the iPhone-dev team’s Subversion repository with svn co http://iphone-dev.googlecode.com/svn/branches/include-1.2-sdk

To use the iPhone-dev team’s headers, you need headers for Mac OS X 10.4 SDK. Then you type this at the command line:

cd /path/to/iphone-dev/svn/branches/include-1.2-sdk
./configure --with-macosx-sdk=/Developer/SDKs/MacOSX10.4u.sdk

sudo bash install-headers.sh

Or you can extract them from the official iPhone SDK. :-)

Once you get the binaries and headers, move them to /usr/local/iphone-sysroot/ . The headers go in /usr/local/iphone-sysroot/usr/include/ , frameworks go in /usr/local/iphone-sysroot/System/Library/Frameworks/ , and libraries in /usr/local/iphone-sysroot/usr/lib/ .

Then to compile and link, set CFLAGS and LDFLAGS like below :

export CFLAGS=-I/usr/local/lib/gcc/arm-apple-darwin9/4.2.1/include \
-isysroot /usr/local/iphone-sysroot

export LDFLAGS=-framework CoreFoundation -framework Foundation \
-framework UIKit -lobjc -bind_at_load -isysroot /usr/local/iphone-sysroot

Then use these compile and linker commands :

/usr/local/bin/arm-apple-darwin9-gcc -c $(CFLAGS) main.c HelloWorld.m

/usr/local/bin/arm-apple-darwin9-gcc $(LDFLAGS) HelloWorld.o main.o \
-o HelloWorld

The easiest way to set this up for each project is in a makefile. Look in the HelloWorld example in the zip file with the slides for an example.

About dreadpiratepj

I have been goofing around with computers since May 1978, when I was about seven years old. For the past decade, I've even managed to have people pay me for this! Suckers! :-)
This entry was posted in iphone, linux, open.source, windows and tagged , , , , . Bookmark the permalink.

13 Responses to iPhone Development on Windows & Linux with open source tools

  1. David says:

    I stumbled upon this page and it caught my interest. I would like to try developing for the iPhone, but I’m very new to development and I’m very new to linux, which is my new operating system that I’m going to stick with.
    With that being said, I have a few questions:
    Which directories do I retrieve from my iPhone? I’m assuming /System/Library/Frameworks and /usr/lib is that correct?
    Also, after running that svn command, where are the headers downloaded to and, what do I do with them from there?
    Is there a special place I put the directories and headers, and is there something I need to do with them and the toolchain in order to start developing?
    Sorry if my questions are “noobish”

  2. David, your questions aren’t noobish at all, and even if they were, you don’t need to apologize. :-)

    You are correct, the binaries you need are /System/Library/Frameworks and /usr/lib . You get these from your iPhone or iPod Touch, from a decrypted system firmware, or from the SDK.

    Once you get the binaries and headers, you can copy them to /usr/local/iphone-sysroot/ . Then to compile and link, set CFLAGS and LDFLAGS like below :

    export CFLAGS=-I/usr/local/lib/gcc/arm-apple-darwin9/4.2.1/include -isysroot /usr/local/iphone-sysroot

    export LDFLAGS=-framework CoreFoundation -framework Foundation -framework UIKit -lobjc -bind_at_load -isysroot /usr/local/iphone-sysroot

    Each export command goes in one long command line, even if it looks like two lines, e.g. export CFLAGS is one line, export LDFLAGS is another separate line.

    Then use these compile and linker commands :

    /usr/local/bin/arm-apple-darwin9-gcc -c $(CFLAGS) main.c HelloWorld.m
    /usr/local/bin/arm-apple-darwin9-gcc $(LDFLAGS) HelloWorld.o main.o -o HelloWorld

    The easiest way to set this up is in a makefile, as in the HelloWorld example in the zip file with the slides.

  3. Lucas says:

    Hi

    When I unpack the toolchain tgz for windows
    I notice that arm-apple-darwin9-g++.exe is zero bytes.
    I guess it is a link to some other file but which?

    /Thanks

  4. Hi Lucas,

    What did you use to unpack the toolchain on Windows? I’m afraid WinZip and WinRAR don’t support symlinks. Try from the cygwin command line:

    cd /

    tar xzf /path/to/toolchain.tgz

  5. Luis says:

    Great info!!! Thanks!!!

    I’ve downloaded everything and gave ‘make’ a try (on a linux (Ubuntu) sstem). I got the include files from the iPhone-dev team SVN repository, but it seems that there are some files missing (or at least not corresponding to your sample code).

    Here are the first few compilation errors:

    /usr/local/bin/arm-apple-darwin9-gcc -c -I/usr/local/lib/gcc/arm-apple-darwin9/4.2.1/include -isysroot /usr/local/iphone-sysroot HelloWorld.m -o HelloWorld.o
    HelloWorld.m:9:34: error: Foundation/Foundation.h: No such file or directory
    In file included from HelloWorld.m:10:
    /usr/local/iphone-sysroot/usr/include/UIKit/UIKit.h:3:38: error: CoreGraphics/CoreGraphics.h: No such file or directory
    /usr/local/iphone-sysroot/usr/include/UIKit/UIKit.h:4:42: error: CoreFoundation/CoreFoundation.h: No such file or directory

    I don’t have a Foundation directory in iphone-sysroot/usr/include, nor a CoreGraphics.

    I suppose I should try getting the includes from the SDK directly?

    Thanks for all this!!!
    Luis.

  6. Hello Luis, thanks for stopping by!

    It seems I forgot a step in using the iPhone-dev team’s header files. You need to provide the location to the Mac OS X 10.4 SDK (the 10.4 SDK that comes with plain XCode 3.1 should be fine). I’ve amended the blog post with the details.

    On the other hard, if you’re going to go through the trouble of obtaining 10.4 SDK files, you may as well just get the official SDK files.

  7. Luis says:

    Managed to get the Mac OX X 10.4 SDK installed using the iPhone-dev instructions plus your ./configure & sudo steps.

    However, that placed all the relevant headers in /usr/local/arm-apple-darwin/include instead of /usr/local/iphone-sysroot/usr/include

    So I copied ..darwin/include/* to iphone-sysroot/usr/include

    Now your HelloWorldiPhone example has a lot less compile errors, but I still get:

    HelloWorld.m:15: error: too many arguments to function ‘UIApplicationMain’

    I wonder if your toolchain download and the iPhone-dev team’s header files are incompatible?

    thanks again!
    L.

  8. Jason says:

    Hi, when linking this page I don’t have much interest. but after reading it, i find it useful.
    and looking at the publishing date 2009-7
    oh, i have to ask a question. will this work for IPHONE3.0?
    i really need a workable toolchain for 3.0

  9. Hi Luis,

    Try the 1.2 SDK headers.

    svn co http://iphone-dev.googlecode.com/svn/branches/include-1.2-sdk

    Sorry for the confusion. It’s been a while since I wrote that HelloWorld example. Notice in the comment header that it’s dated August 2008.

  10. Jason, I had someone write to me in the last few days that the toolchain does not work with 3.0. I’m looking into it but don’t have a timeframe. Will get to it when I can. My hunch is that 3.0 uses GCC 4.2 and this toolchain is GCC 4.0, but it’s just a hunch.

  11. jason says:

    I have tried hard to use gcc4.2 to build the toolchain, and i did follow the instruction of saurik http://www.saurik.com/id/4

    but, unfortunately i failed. Contacting with saurik, he just told me that the toolchain will work, but don’t tell me how to make it work. i feel sad, and i am really disappointed because he didn reply my Email anymore

  12. Pete Bones says:

    I’ve been toying with the idea of trying out iPhone dev for a while now, and my question is this:

    if I start a project using the open source SDk, how easy is it to transfer it to the official SDK, should I decide to do so? Thanks!

  13. Hi Pete,

    The code you write to program with the open source toolchain is exactly the same as with the official SDK, as long as you use the SDK headers, frameworks and libraries. You should be able to get started with a Windows or Linux computer and a jailbroken device.

    I’ll argue that going this route will make you a better iPhone developer, because you’ll learn how to program your UI from scratch, instead of being spoiled by Interface Builder. ;-)

Comments are closed.