So, without further delay here's what I did:
- Start with following the directions on antariz's blog: http://antirez.com/page/iphone-gcc-guide.html. 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 http://www.megaupload.com/?d=55ZNOCKI and put them on your phone somewhere.
- 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 http://modmyi.com/forums/iphone-ipod-touch-sdk-development-discussion/655111-compiling-iphone-3-0-a.html, download fake-libgcc at http://files.getdropbox.com/u/876743/fake-libgcc_1.0_iphoneos-arm.deb, and install it with dpkg -i fake-libgcc_1.0_iphoneos-arm.deb. Then you can install GCC from cydia.
- 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.
- 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() {
printf("Hello\n");
return 0;
}
--------------
Save it as hello.c, or whatever you want. - Build the program in the standard way:
----------------------------
$> gcc hello.c -o hello
----------------------------- - When you try to execute the program with ./hello now, you will probably get the message:
-------------------
$> ./hello
Killed
-------------------
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 - Now you can run your program normally.
- 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!
- 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
---------------------------- - 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
Hello
Have fun!