Sunday, October 18, 2009

Building c/c++ applications using GCC/G++ on iPhone

In order to save people the trouble that I had to go through, I am going to describe how I was able to build c and c++ code on the jailbroken iPhone, 3.0 firmware, using Cydia, and the GNU toolchain with gcc, g++, and make, as opposed to XCode. I'm a Linux user and don't have a Mac computer, so I'm not able to install and use the iPhone SDK, not to mention I don't feel like registering for all kinds of Apple junk.

So, without further delay here's what I did:
  1. Start with following the directions on antariz's blog: Do what he says, including jailbreaking the phone, install the build tools in cydia which are mentioned (however, GCC won't work, so see #2), and download the header files from and put them on your phone somewhere.
  2. There are some problems with the antirez instructions. The first one is that GNU C Compiler cannot be installed in cydia because libgcc cannot be installed. Following the advice on, download fake-libgcc at, and install it with dpkg -i fake-libgcc_1.0_iphoneos-arm.deb. Then you can install GCC from cydia.
  3. The downloaded header files need to be placed in a standard include directory. After you extract the include files onto your phone, you need to copy all of them to /usr/include.
  4. Ok, now you should be ready to build C program (C++ will come after.) So, make a simple hello world program:
    #include <stdio.h>
    int main() {
    return 0;
    Save it as hello.c, or whatever you want.
  5. Build the program in the standard way:
    $> gcc hello.c -o hello
  6. When you try to execute the program with ./hello now, you will probably get the message:
    $> ./hello
    This is because you need to sign applications written on the iPhone for whatever reason. To do this, you use the program ldid:
    $> ldid -S ./hello
  7. Now you can run your program normally.
  8. Next challenge is building C++ apps. EDIT: There are some c++ dependencies needed from Cygwin which were not mentioned in antariz's blog but are apparently needed. I have the following packages installed from Cydia, so not sure which are necessary and which aren't but you can try installing them:
    • C++ Standard Library
    • iPhone 2.0 Toolchain (thanks welf)
    • iPhone OS C/C++ Compiler
    • libsigc++
    • anything else that you can find in Cydia related to c++ if things don't work for you!
  9. By default g++ doesn't know where to look for the stl libraries, such as iostream etc. You need to set this up yourself. To do so, find where iostream is on your system by running:
    $> find / -name "iostream" -print
    It is in a whole bunch of places on my phone. /private/var/include/c++/4.0.0 seems like the most reasonable one, so I use that one. We need to add it to the standard C++ include dirs, so to do this we add to the environment variable CPLUS_INCLUDE_PATH. Another thing that g++ complains about needing is bits/c++config.h. For me it is in /private/var/include/c++/4.0.0/i686-apple-darwin9. So, we add those two directories to the CPLUS_INCLUDE_PATH:
    $> export CPLUS_INCLUDE_PATH=/private/var/include/c++/4.0.0:/private/var/include/c++/4.0.0/i686-apple-darwin9
  10. Now we can build C++ apps, since it can find the stl libraries. So if you have the source file:
    #include <iostream>
    using namespace std;
    int main() {
    cout << "Hello\n"; return 0; } --------------

    Now build and run it with:
    $> g++ hello.cpp -o hello
    $> ldid -S ./hello
    $> ./hello
So there you go. Hopefully it works out for you. Note I was using iPhone OS 3.0. All commands were run using ssh into the iPhone. If you do it from the Terminal.App installed by cydia, make sure you run all commands as root, otherwise you may have permission problems to sort out.

Have fun!