Cocotron and Xcode4: How to Run Objective-C Apps on Linux

May 1st, 2011 · 14 Comments · Objective-C, Tutorials

An updated version of this article is available for Ubuntu Linux 12.04 (32 bit).

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.

  1. Create directory structure /Developer/Cocotron/1.0/PlatformInterfaces.
  2. In the PlatformInterfaces directory, create a subdirectory named i386-ubuntu-linux.
  3. From your Ubuntu machine, copy the /usr/include directory to /Developer/Cocotron/1.0/PlatformInterfaces/i386-ubuntu-linux/include on your Mac.
  4. 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.
  5. In the i386-ubuntu-linux directory, create a subdirectory named lib.
  6. From your Ubuntu Linux machine, copy the following files to that directory from /usr/lib/i386-linux-gnu:

    crt1.o
    crti.o
    crtn.o
    libc.a
    libc.so
    libc_nonshared.a
    libdl.a
    libdl.so
    libm.a
    libm.so
    libpthread_nonshared.a
    libpthread.a
    libpthread.so

  7. Additionally, copy the following files to lib from your Ubuntu’s lib/i386-linux-gnu directory:

    libc-2.13.so
    libc.so.6 -> libc-2.13.so
    ld-2.13.so
    ld-linux.so.2 -> ld-2.13.so
    libdl-2.13.so
    libdl.so.2 -> libdl-2.13.so
    libpthread-2.13.so
    libpthread.so.0 -> libpthread-2.13.so

  8. 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]
  9. Run install.sh from your InstallCDT directory as follows:
    $ sudo install.sh Linux i386
  10. 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.

Tags: ···

14 responses so far ↓

  • 1 Ross Hunter  May 6, 2011 at 1:11 am

    Looking forward to your next post.

  • 2 Mike  May 6, 2011 at 3:19 pm

    Good post – also looking forward to the next one!

  • 3 Christian  May 26, 2011 at 11:25 am

    Hi there.

    Thanks for your tutorial. I’m however stuck at step 9 (installing CDT). I do get errors about binutils not findung libiconv-la… :-(
    /bin/sh ./libtool –tag=CC –mode=link gcc -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Werror -m32 -Wformat=0 -Wno-error=deprecated-declarations -o size size.o bucomm.o version.o filemode.o ../bfd/libbfd.la ../libiberty/libiberty.a -lintl -lz
    libtool: link: cannot find the library `/usr/lib/libiconv.la’ or unhandled argument `/usr/lib/libiconv.la’

    Not sure how to fix that any hint would be greatly appreciated.

  • 4 Tobias  Jun 4, 2011 at 2:09 pm

    Hi Christian,

    I have MacPorts installed on my system and this contains libiconv.la. I am not really sure since I cannot reproduce the issue on my system, but maybe this helps.

    Best,
    Tobias

  • 5 Cocotron and Xcode4: How to Run Objective-C Apps on Linux (Part 2)  Jun 7, 2011 at 11:22 pm

    [...] ← Cocotron and Xcode4: How to Run Objective-C Apps on Linux [...]

  • 6 Praveen R  Jul 9, 2011 at 1:24 pm

    Out of anxiety, added this question to quora also, http://www.quora.com/Could-all-Mac-Apps-be-ported-to-other-Platforms-like-Windows-Linux-using-Cocotron

  • 7 Tobias  Aug 1, 2011 at 11:19 pm

    I suspect the question is a little too broad to be responded with a single answer. Of course you could — in principle — port any Mac app over to Windows or Linux based on Cocotron. However, it depends on how much effort you are willing to put into it. Do you have a particular case at hand? Is it just about the UI layer? As I already said on the Part 2 post comments, I’m afraid there is no easy-to-use UI framework readily available for Linux when it comes to Objective-C. At least not, if you want it to be comparable to what is currently possible with Cocoa on OS X or Qt on Linux, for example.

  • 8 Gregory Casamento  Aug 16, 2011 at 5:12 pm

    With GNUstep you can actually run applications instead of just command line tools.

    GC

  • 9 andyvn22  Oct 21, 2011 at 2:05 pm

    Nice tutorial! Anyone know how to avoid the following during the CDT install?

    /Developer/Cocotron/1.0/Linux/i386/gcc-4.3.1/i386-ubuntu-linux/lib//libm.so: file not recognized: File format not recognized
    collect2: ld returned 1 exit status

  • 10 Martijn The  Jan 13, 2012 at 7:17 pm

    @andyvn22, I had the same problem as you had.

    First I thought it had something to do with binutils, so I changed the script to use a slightly newer version. Then I tried to use one of binutils’ programs on the symlink that the error referred to: presto, apparently the binutils program(s) don’t handle the symlink properly.

    So what I did in the end was, to make hard links instead of symlinks (e.g. using `ln libm-2.5.so libm.so`).

    Hope it helps!
    Martijn

  • 11 Dave  Feb 16, 2013 at 12:10 am

    I followed the instructions on this site (thank you!) and also added the /usr/include to my mac from Ubuntu 12.10 as well. It gets close, then dies on :
    configure: error: C preprocessor “/lib/cpp” fails sanity check
    See `config.log’ for more details.
    make[1]: *** [configure-target-libgcc] Error 1
    make: *** [all] Error 2

    Any ideas? Trashed the old linux build folder, installed dev tools, reboot, nada.

  • 12 Tobias  Feb 17, 2013 at 11:28 pm

    The /lib/cpp fails sanity check error is likely misleading. Please check config.log. You will probably see a bunch of errors related to header files. (At least this is what I got last time I tried to cross compile for Ubuntu 12.) If so, you need to copy those headers to a different directory, so that the compiler can find them. If you need immediate help, please mail the config.log file.

    Btw.: I will soon update the article w.r.t. Ubuntu 12.

  • 13 Dave  Feb 18, 2013 at 3:48 am

    Hi Tobias,
    I was able to get the CDTools to install without error. I had no /lib directory on my disk (or SSD. What’s a disk these days) so on someone else’s recommendation I created a symlink to /usr/lib. I also had to create another link inside usr/lib:

    i386-linux-gnu -> /Developer/Cocotron/1.0/PlatformInterfaces/i386-ubuntu-linux/lib

    This got CDT to finish without error, but when I was done I still couldn’t build Foundation for Linux, it fails first with not being able to find pthread.h, but I’m sure this is the tip of the iceberg.

    I’m in no terrible rush, a couple of weeks would be fine. I’d be happy to test or send you stuff that I’m stuck with- greatly appreciate your help. Feel free to email me directly. Thank you!

  • 14 Compiling Cocotron Foundation Framework for Ubuntu 12.04  Feb 20, 2013 at 12:48 am

    [...] Cocotron toolchain and Foundation framework for Ubuntu 12.04 (32 bit). Most instructions from the old article for Ubuntu 11 are still valid, however, there are a couple of changes, both for the Cocotron Developer Tools [...]

Leave a Comment

Before you comment: please be kind and save me some time by NOT posting inappropriate content. Example: if you're religious that's fine. But it doesn't belong here! Comments should generally be related to the topic of the article. Thanks!

© 2013 Tobis Lensing. All rights reserved. Powered by Wordpress — based upon Cutline by Chris Pearson. This page reflects the personal opinion of the author and is in no way linked to institutions the author is working or has worked for. For more information, see the disclaimer.