Jailbreaking iPhone: How to Install GCC 4.2 on iPhone

Following this blog post: http://blog.syshalt.net/index.php/2010/09/12/compile-c-applications-with-gcc-on-ios-4-iphone/, I installed GCC 4.2.1 on my iPhone and gcc works.  However, g++ does not.  Moreover, above post does not explain how comes the header files and library files for gcc on iPhone.

This post complements above post by adding the part about C++.  First of all, I copy-and-pastes above post.

———————————————-

Here is what you need to do in order to be able to compile and run a C application on iphone:

1. You will need to Jailbreak the iPhone first, search on google for more info.
2. Install OpenSSH from Cydia.
3. Connect iPhone to your wireless network and SSH to it.
4. Download this application using: wget http://www.syshalt.net/pub/iphone/gcc-iphone/fake-libgcc_1.0_iphoneos-arm.deb
5. Install libcc using: dpkg –i fake-libgcc_1.0_iphoneos-arm.deb
6. Install iphone-gcc using this command: apt-get install iphone-gcc
(you can download this version from my website: iphone-gcc if does not work on your device the one that is installed by default)
7. Download using: wget http://www.syshalt.net/iphone/gcc-iphone/sdk-2.0-headers.tar.gz
8. Untar with command: tar -xvzf sdk-2.0-headers.tar.gz
9. Enter in the new created folder with: cd include-2.0-sdk-ready-for-iphone
10. Copy all files to include folder with command: cp –r * /usr/include
11. Now type: cd .. in order to return to the previous folder
12. Download using: wget http://www.syshalt.net/iphone/gcc-iphone/gcc_files.tar.gz
13. Untar with command: tar -xvzf gcc_files.tar.gz
14. Enter in the new created folder with command: cd gcc_files
15. Copy all files to /usr/lib using command: cp –r * /usr/lib
16. Install ldid to sign the application (this will prevent iOS to kill the application at startup) using: apt-get install ldid
17. Sign your compilet aplication using: ldid –S <application>
18. Run the application using: ./<application>

——————————————–

From step 9 to step 15, it explains how to install C header files and library files. These files can be copied from the iPhone SDK. On my MacBook, I have Xcode 4.1 and iPhone SDK 4.3, which resides in the directory:

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk

I just package the usr subdirectory and throw it to my iPhone:

tar cjf iPhoneOS4.3.sdk-usr.tar.bz2 \
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr
scp iPhoneOS4.3.sdk-usr.tar.bz2 mobile@:/var/mobile/Media/

Then, on my iPhone, I unpack the tarball and installed the packing using the following commands:

cd /var/mobile/Media
tar xjf iPhone4.3.sdk-usr.tar.bz2
cd /usr/local
sudo ln -s /var/mobile/Media/iPhoneOS4.3.sdk-usr/include
cd /usr/lib
sudo ln -s /var/mobile/Media/iPhoneOS4.3.sdk-usr/lib/libSystem.B.dylib
sudo ln -s /var/mobile/Media/iPhoneOS4.3.sdk-usr/lib/libSystem.dylib
sudo ln -s /var/mobile/Media/iPhoneOS4.3.sdk-usr/lib/libgcc_s.1.dylib
sudo rmdir system
sudo ln -s /var/mobile/Media/iPhoneOS4.3.sdk-usr/lib/system
for i in  /var/mobile/Media/iPhoneOS4.3.sdk-usr/lib/libstdc++*; do \
sudo ln -s $i; done

It is now the time to test g++. Using the following command:

g++ hello.cc -o hello \
-I /usr/local/include/c++/4.2.1/ \
-I /usr/local/include/c++/4.2.1/armv7-apple-darwin10

I successfully built the following simple C++ program:

#include <stdio.h>

int main() {
        std::cout << "Hello World!";
        return 0;
}