This is part one of a two part tutorial. The first part focusses on cross compiling the Cocotron Foundation framework. The second part explains how to get an exemplary CLI app up and running on Linux.
Part 1: Cross Compiling Foundation
Everyone who knows me knows I’m an advocate of Objective-C, the Foundation framework, and Cocoa. Luckily, in recent years Objective-C has gained wide-spread popularity by virtue of the iPhone/iPad hype. However, there has always been a lack of integration with operating systems not originating from Apple. There are numerous reasons one would want to compile Objective-C based code on Linux, for instance. Imagine you are writing an iPhone multiplayer game and you need a custom server solution for that. Chances are you will want to reuse parts of your client code, but you won’t run an OS X based server farm. If so, you will quickly feel the need for porting lots of Objective-C code over to another language, say C/C++, simply because there is no (easy) way to get your original code working on the server platform.
Fortunately, I am not the only one thinking in that direction and so Christopher Lloyd came up with his great Cocotron project. Cocotron provides an open source implementation of the most important frameworks used with Objective-C on the Mac and iOS platforms. What is more, it comes with a cross compiler integration for Xcode. This allows you to build Objective-C based apps for a number of operating systems, e.g. Windows or Linux.
Both Cocotron and Apple are working constantly, so there is need for a good tutorial of how to get started with Cocotron and the newest Apple developer tools on Linux. This is what this article is all about.
Things You Will Need
- A Mac with OS X 10.6.7+ and Xcode4 (latest developer tools from Apple) installed; you will use this for cross compilation
- A machine with your target Linux OS installed (we will use Ubuntu 11.04 32-bit here)
- The Cocotron source code (get it by typing “$ hg clone http://cocotron.googlecode.com/hg/ cocotron” on the terminal)
- The Cocotron Developer Tools (CDT)
Step 1: Install Cocotron Developer Tools (CDT)
Cocotron’s InstallCDT provides an automatized setup and Xcode integration of tool chains for Objective-C cross compilation. Download the latest InstallCDT package and unzip it to a directory on your Mac.
Step 2: Create a Platform Interface for Cocotron Developer Tools
Installing CDT requires some files from your target platform. We are going to get these in this step. Don’t worry, this is already the most complicated part of the story.
- Create directory structure /Developer/Cocotron/1.0/PlatformInterfaces.
- In the PlatformInterfaces directory, create a subdirectory named i386-ubuntu-linux.
- From your Ubuntu machine, copy the /usr/include directory to /Developer/Cocotron/1.0/PlatformInterfaces/i386-ubuntu-linux/include on your Mac.
- Additionally, from your Ubuntu machine, copy the /usr/src/linux-headers-x.x.xx-x/arch/x86/include/asm diectory to /Developer/Cocotron/1.0/PlatformInterfaces/i386-ubuntu-linux/include/asm on your Mac.
- In the i386-ubuntu-linux directory, create a subdirectory named lib.
- From your Ubuntu Linux machine, copy the following files to that directory from /usr/lib/i386-linux-gnu:
- Additionally, copy the following files to lib from your Ubuntu’s lib/i386-linux-gnu directory:
libc.so.6 -> libc-2.13.so
ld-linux.so.2 -> ld-2.13.so
libdl.so.2 -> libdl-2.13.so
libpthread.so.0 -> libpthread-2.13.so
- Edit libc.so and libpthread.so using a text editor and replace /usr/lib/i386-linux-gnu and /lib/i386-linux-gnu paths with /Developer/Cocotron/1.0/PlatformInterfaces/i386-ubuntu-linux/lib. [Source]
- Run install.sh from your InstallCDT directory as follows:
$ sudo install.sh Linux i386
- Finally, change ownership of the /Developer/Cocotron directory as follows:
$ sudo chown -R YOUR_USER_NAME:admin /Developer/Cocotron
This will recursively change ownership of /Developer/Cocotron to your user (needed for use with Xcode4.)
Step 3: Getting the Cocotron Source
You can get the Cocotron source by typing the following in a terminal session:
$ hg clone http://cocotron.googlecode.com/hg/ cocotron
Step 4: Compiling Foundation
As an example, let’s compile the Cocotron Foundation project for Ubuntu. Open Xcode4 and load Foundation.xcodeproj from the Cocotron source code. Choose ‘Foundation-Linux-i386’ as active scheme. Build the Cocotron Foundation framework by hitting Cmd+B. If you see ‘Build succeeded’, congratulations, you have taken the first step towards Objective-C on Linux.
Note: the steps described here relate to Ubuntu Linux (11.04). Creating a CDT platform interface for a different platform may require other steps. For more information see Creating a Platform Interface for CDT.
In the next post I am going to make this tutorial complete by explaining how to set up a Cocotron based Xcode project from scratch. That tutorial will also cover installing and running your first Objective-C binary on Ubuntu Linux.