Creating an Objective-C based CLI for Ubuntu 12 using Cocotron

February 24th, 2013 · 2 Comments · Objective-C, Tutorials

In Part 1: Compiling Cocotron Foundation Framework For Ubuntu 12.04 I provided an updated step by step tutorial on how to compile the Cocotron Foundation framework for Ubuntu 12.04 using Xcode 4.

This post is an updated version of the old part 2 tutorial on how to cross compile a simple command line tool runnable on Ubuntu 12.

Step 1: Creating the Xcode Project

The first thing we will do is create a new command line tool Xcode project.

  1. Open up Xcode and create a new project by selecting “File” => “New” => “New Project…” from the main menu.
  2. Create a new command line tool project by selecting “Application” => “Command Line Tool” from the new project template wizard.
  3. Name the project “CocotronUbuntuCLI” and choose “Foundation” from the type dropdown.

Step 2: Replacing OS X with Linux Foundation Framework

The standard project template used by Xcode will reference Apple’s Foundation framework, which of course won’t run on Linux. Hence, we will have to replace it with the Cocotron Foundation framework that you built in part 1 of this tutorial.

  1. In the project navigator, expand the “Frameworks” item and remove “Foundation.framework” by selecting it and hitting backspace.
  2. To add the Foundation framework for Linux, right click on the project navigator, select “Add Files to CocotronUbuntuCLI…” from the contextual menu, and locate Cocotron’s Foundation.framework that you built in the last part of this tutorial. Usually this will be located at /Developer/Cocotron/1.0/Linux/i386/Frameworks/ Foundation.framework.
  3. Finally, click “Add”.

Step 3: Replacing Unsupported @autoreleasepool Statement

Unfortunately, Cocotron does not support some of the syntactic sugar provided by modern Objective-C versions.

In the main.m source file, change

@autoreleasepool {
    // your code
}

to

NSAutoreleasePool *pool = [NSAutoreleasePool alloc] init];
// your code
[pool release];

Step 4: Configuring the Target’s Build Settings

In order to make Xcode build the application correctly, we will have to change some build settings.

  1. From the project navigator, click on the “CocotronUbuntuCLI” project item. This will open up project settings in the editor.
  2. In the project settings editor, select the “CocotronUbuntuCLI” target from the tree control on the left. Then select “Build Settings” => “All”/”Combined” from the project settings editor’s tabs.
  3. Change “Architecture” to “32-bit Intel”.
  4. Change “Mac OS X Deployment Target” to “Compiler Default”.
  5. Change “Compiler for C/C++/Objective-C” to “Cocotron 1.0 Linux i386 gcc 4.3.1”.
  6. Change “Implicit Conversion to 32 Bit Type” to “No”.

Step 5: Installing and Testing the Command Line Interface on Ubuntu Linux

  1. Locate Cocotron’s Foundation.framework directory on your Mac (see step 2.2.)
  2. Copy the complete directory to /usr/lib on your Ubuntu Linux machine recursively. Make sure to preserve symlinks.
  3. Within your Linux system’s /usr/lib directory, create a symlink named “libFoundation.so” to /usr/lib/Foundation.framework/libFoundation.so.
  4. Copy the build product from step 5.4 to your user’s home directory on the Ubuntu Linux machine.
    Finally, open up a terminal on your Linux and execute the command line tool by typing ./CocotronUbuntuCLI (current working directory must be your home directory.) You should see an NSLog message saying “Hello, World!” if everything worked correctly!

Limitations

Unfortunately, Cocotron did not yet migrate to LLVM and does therefore not support some of the new compiler and language features of Objective-C.

Most notably, the following is NOT supported when cross compiling with Cocotron:

  • Blocks
  • Grand Central Dispatch (GCD)
  • ARC
  • @autoreleasepool, @synchronized, etc.

See what you can do about it!

Tags: ···

2 responses so far ↓

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.