Blog: C/C++: Compiling on iOS

C/C++: Compiling on iOS

C language is great because it’s one of the most used today and has compilers widely available for most architectures. The propose of this post is to explain how to compile your own C/C++ projects under the new iOS versions like 3, 4 and 5.x.

Nowadays, compiling C/C++ for iPhone, isn’t to easy like before, there are three major problems:

- No LibGCC available on Cydia. The famous GCC Compiler has been ported to the iPhone (you can found it as GNU C Compiler on Cydia). GCC depends on LibGCC  witch is no longer available on Cydia (since iOS 2.0) so it’s impossible to install CGG.

- The C Standard Library is not completely installed on iOS.

- Apple Code Sign. Saurik (Cydia developer) has already explain that…

“Starting with the recent beta releases of the iPhoneOS, Apple has started requiring that all code on the device is signed. This is mostly to make it impossible for programs running through Apple’s AppStore to download more software and run it (so no competition for AppStore).”

Fortunately right now an compatible with iOS 5 there’s an open source project that aims to port GCC and it’s dependencies to the iPhone. The project is called iPhone-GCC-Full.

To start you’ll need to get some components:

  • CSU – Go to Cydia and search and install csu.
  • Link Identify Editor (aka ldid) – Go to Cydia and search and install ldid.
  • LibGCC: http://code.google.com/p/iphone-gcc-full/downloads/detail?name=libgcc.deb
  • GCC: http://code.google.com/p/iphone-gcc-full/downloads/detail?name=iphone-gcc.deb
  • Headers/Libraries: http://code.google.com/p/iphone-gcc-full/downloads/detail?name=headers-libs.deb

After downloading those components, launch Cyberduck or your favorite SFTP software and upload the files into /private/var/root on your iPhone.

Now, connect to your iPhone via SSH using Terminal in your Mac and install the downloaded packages:

your-mac:~ you$  ssh root@your-iphone.local

root@your-iphone.local’s password: : …   // The default password is “alpine”

your-iphone:~ you$ dpkg -install libgcc.deb

your-iphone:~ you$  dpkg -install iphone-gcc.deb

your-iphone:~ you$  dpkg -install headers-libs.deb

Now you can compile your applications like you do in your computer, by issuing for e.g. “cpp -o helloworld helloworld.c”.

After compiling if you try to run your app iPhone just says “Killed” when you launch it! This is Code Sign working… In order to solve this issue it’s necessary to sign the application. There are 3 options to sign apps or make it run but, the best option is called Pseudo-Signing:

“To get around this, I wrote a tool called ldid that, among other things, can generate the SHA1 hashes that are checked by Apple’s iPhoneOS kernel” – (The other signing options are here: http://www.saurik.com/id/8)

Issue the following on your iPhone Terminal:

your-iphone:~ you$ ldid -S helloworld

Now if you try to run your app it will work!! Enjoy the freedom of having your small or huge utilities running in your pocket anytime you need them! Don’t forget to install MobileTerminal to run your Apps.

Hope this guide was helpful!

9 Comments

Leave a Comment