3 min read

Compiling Our First C Binary - iOS Edition

Compiling Our First C Binary - iOS Edition

Today we’re going to take a little look at developing for our iPhone using the C Programming Language.

A great feature of C Programming is that it’s portable across a wide range of devices, and so the C code which is running on the Mac can be compiled and run on our iPhone!

If you haven’t checked out my Introduction To C Programming Course, please do as it’ll give you a nice introduction where you can use those skills in this article! -> https://www.youtube.com/playlist?list=PLAkw4Bz_zm0HA1JFKmwEvp-isz25S8dFg

I’ll presume you have a little basic knowledge working with C, so without further ado, let’s get started preparing our iPhone!

Let’s connect to our iPhone via SSH so we can establish a terminal session to our device from the mac...

I personally use iPhone Tunnel on the Mac to establish a connection via USB (https://code.google.com/archive/p/iphonetunnel-mac/downloads).

Download iPhone Tunnel from that URL! (iPhone Tunnel 2.3 PE beta 1 works perfectly okay for me!)

Now go ahead and open the DMG and double click the binary. You won’t see any visual response on your display - this is completely expected. Check your menu bar, hit the icon and select Preferences.

Checkra1n uses Port 44 for USB-SSH functionality, but the default in iPhone Tunnel is Port 22. Modify the Device Port from 22 to 44, close the preferences window, head back to the menu icon.....drum roll..... Turn Tunnel On!

Woohooo!, we’re ready to connect! Head back to our too now-well-known menu icon, and browse to ‘Tools’ -> ‘SSH as root’. You may have to click Allow in the permissions prompt for the Terminal Window to appear.

Type ‘yes’, hit enter, and use the password ‘alpine’. You’re in! 🔥

Packages, repositories, and SDKs…!

Back to the iPhone 📲. Head to Cydia, Search, and let’s find ‘LLVM+Clang’ and install it. This could take a little while, so do bear with it! This package will install the compiler (which translates our C code to object files, which will then be used as an input for the ‘linker’) and a few other necessary utilities.

Now it’s time to install ‘ld64’ from Cydia. A linker takes in the ‘object’ files and combines them into a single executable that can be directly executed by our iDevice.

We now have to download the iOS SDK in order for the required libraries to be available to the compiler (you can learn more about libraries in my Introduction To C Course!):

- Download the SDK on your Mac ( https://codeload.github.com/mstg/iOS-full-sdk/zip/master )

- Extract the zip!

- Use the following command from the Mac terminal to transfer the SDK from your Downloads folder to the ‘root’ of your iPhone ( scp -r -P 2222 ~/Downloads/iOS-full-sdk-master/iPhoneOS9.3.sdk [email protected]:/iPhoneOS9.3.sdk ).

Congratulations! We are now ready to compile our first C binary on the iPhone .

Write some code!

Write some C code of your choice, maybe a simple helloworld C file. To transfer the file to your iPhone, execute the following command from the Mac, replacing the first FILE with the absolute directory reference of the .c file, and the final FILE being just hello.c ‘scp -P 2222 FILE [email protected]:/FILE’.

I’ve uploaded a simple hello.c file for you to test with!

It’s compiling time!

Finally, let’s head back to our SSH Terminal Session with the device. Execute the following:

cd /’ - This moves our ‘working directory’ to the root of the iPhone Drive.

clang FILE -isysroot /iPhoneOS9.3.sdk -o out

Now, finally, all that’s left is to ‘sign’ our binary so it can execute! run the following command exactly, replacing out with the output binary from the previous step. ‘ldid -S out’.

Congratulations! 🥳 You should now be able to successfully execute the C code by typing ./out

Please do let me know if this article could do with any extra clarifications or if you have any questions at all! I am always happy to help out if I can :)