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: 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.
  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 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.
  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() {
    printf("Hello\n");
    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
    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
  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
    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!

Tuesday, May 6, 2008

To replace strings in multiple files, do the following:

$find -name '*.txt' -print0 | xargs -0 perl -pi -e 's/old/new/g'

Sunday, March 9, 2008

Fonts in Fedora 8

First of all, I had to install OpenOffice in my Fedora system, KWord just couldn't cut :( But today, I noticed when creating a document that none of my favourite fonts were available, such as Arial or Times New Roman. It turns out these are considered Windows fonts and aren't included. Since I collaborate with a lot of people who use Windows, it is essential to have these fonts available. Here is how you do it (courtesy of the Fedora FAQ at http://www.fedorafaq.org/#installfonts):

1. Open a Terminal.

2. Become root:
$> su -

3. Download the MS Core Fonts Smart Package File:
$> wget http://corefonts.sourceforge.net/msttcorefonts-2.0-1.spec

4. Make sure that the rpm-build and cabextract packages are installed:
$> yum install rpm-build cabextract

5. Build the Core Fonts package:
$> rpmbuild -ba msttcorefonts-2.0-1.spec

6. Install the Core Fonts package:
$> yum localinstall --nogpgcheck /usr/src/redhat/RPMS/noarch/msttcorefonts-2.0-1.noarch.rpm

Saturday, March 8, 2008

Fedora 8 Rules

I installed Fedora 8 a few days ago with KDE, and I just have to say that it is AMAZING. Previously I had been using Ubuntu. I was and still am a huge fan of Ubuntu, as it was the first distribution that let me completely break free of the clutches of Windows. However, Ubuntu was just crawling while working on a Java project in eclipse this week on my laptop. I actually got so fed up that I did the unthinkable, booted into Windows XP to work!

After going to bed feeling filthy, I woke up and decided to try out the Fedora 8 CD that came with my Linux Pro magazine. The CD was defective of course, so I proceeded to download the KDE live cd off of the Fedora website.

Installation went OK, although not as smoothly as Ubuntu's. However upon booting up I was instantly impressed. Not only did it look great, the thing that really caught me was at the lightning-fast responsiveness of the interface. The same laptop that was chugging with Eclipse and Firefox going at the same time now handles the task with ease and is way more responsive as a whole.

Anyway, long story short, what I really wanted to do is list some of the things that make Fedora 8 / KDE awesome:

  1. Speed: the interface kills anything else I've seen in terms of speed & responsiveness by far, including Windows XP, and Ubuntu with both Gnome and KDE.
  2. Looks: it looks very slick. Also comes with compiz fusion, which just so happens to work without any extra configuration for me (although I turned it off since speed is king for me)
  3. Software installation: Fedora is based on Red-Hat RPM, which is more supported and widely available than debian DEBs, and makes software just as easy to install as a Windows software, which is most peoples' argument against Linux.
  4. Multimedia: although setting up Mp3 support is a pain in the ass because of patent garbage, I managed to get it set up by doing things that a non-expert would be able to do. Once that was done, the multimedia apps RULE. Amarok for example kills any media player that I've ever seen: it's fast, lightweight, looks great, etc.
  5. Configuration: modifying xorg.conf is a major hastle with ubuntu. Whenever i'd screw it up, I'd have to reboot my computer, go into safe mode, blah blah. With Fedora you just back out of X (ctrl-backspace), change your xorg.conf, then get back into X with startx. The way linux is supposed to be, not everything all mashed into X-windows like ubuntu was.
  6. Development: setting up development tools is very easy. Also, because it's based on RPM you can get more current releases of software than with DEBs.
  7. Document viewing/editing: I must say I am very impressed with the KDE office suite. Haven't used it extensively, but so far it seems much better than the bloated pig that openoffice is. Time will tell whether or not I will need to install openoffice for compatibility reasons... As for viewing, KPDF is a remarkable piece of software. Destroys evince, and even Adobe Acrobat if you ask me. Even has text to speech via kttsmgr.
  8. Probably a lot more, but I have to get back to work now.
Later
\jeremy

Search for a string in a list of files with grep/bash

Today I was trying to find which file the main() function was in, in a source code directory for a project that I'm looking at. To do this, you would do the following: and that's it

$> grep -lir "main" *

and that's it. So just call grep with -lir and the search string you're looking for, and in the files you're interested in (all files in the current directory in my case). The grep params are:

-l: outputs on the file name where the string occured
-i: ignore the case
-r: recursively look into all directories

Damn I love grep...

First post

I set up this blog basically so that I can track my computer related research discoveries somewhere, so that when other people out there come across the same problems, they'll be able to do a search in google and come across my solutions, without having to go through the same pain and struggle to find it that I did. Countless times search engines have come to my rescue when trying to solve some difficult problem, so now it's my turn to give something back. Hope you enjoy, feel free to drop me a line if you have questions or comments about something. /jeremy