Part 2: Creating a Foundation-based CLI for Ubuntu
In Part 1: Cross Compiling Foundation I provided a step by step tutorial on how to compile the Cocotron Foundation framework for Ubuntu Linux using Xcode 4.
This post is intended to complete the tutorial by explaining how a simple command line tool can be created which will run on Ubuntu Linux using the previously compiled Foundation framework.
After completing this tutorial you will have:
- a baseline project for compiling (GUI-less) Objective-C apps on Linux,
- Foundation.framework installed on your Ubuntu Linux machine,
- basic support for NSUserDefaults on the Linux machine.
The requirements for completing this tutorial are basically the same as those provided in the previous part. What is more, you need to complete part 1 successfully before you may start with part 2.
Step 1: Creating the Base Xcode Project
The first thing we will do is create a new command line tool Xcode project.
- Open up Xcode and create a new project by selecting “File” => “New” => “New Project…” from the main menu.
- Create a new command line tool project by selecting “Application” => “Command Line Tool” from the new project template wizard.
- 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.
- In the project navigator, expand the “Frameworks” item and remove “Foundation.framework” by selecting it and hitting backspace.
- 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.
- Finally, click “Add”.
Step 3: Configuring the Target’s Build Settings
In order to make Xcode build the application correctly, we will have to change some build settings.
- From the project navigator, click on the “CocotronUbuntuCLI” project item. This will open up project settings in the editor.
- 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.
- Change “Architecture” to “32-bit Intel”.
- Change “Mac OS X Deployment Target” to “Compiler Default”.
- Change “C/C++ Compiler Version” to “System default (LLVM GCC 4.2)”.
- Change “Implicit Conversion to 32 Bit Type” to “No”.
Step 4: Configuring the Target’s Build Rules and Building the Source
Additionally, we have to add a build rule for C source files, so Xcode uses the correct compiler tool chain to build our command line tool for Linux.
- In the project settings editor, select the “CocotronUbuntuCLI” target from the tree control on the left. Then select “Build Rules” => “Target” from the project settings editor’s tabs.
- Click the “Add Build Rule” button on the lower right corner of the Xcode window. A new build rule is added to the target.
- From the “Process” dropdown select “C source files”; from the “Using” dropdown, select “Cocotron 1.0 Linux i386 gcc default (4.3.1)”.
- Finally, build the command line tool by hitting Cmd+B.
Step 5: Installing libm.so.6 on Your OS X Machine
The last step to set up the project for proper building is to get a Foundation framework dependency from your Ubuntu machine, namely libm.so.6. This library was not needed in part 1, but is required when linking against Cocotron’s Foundation.framework. In fact, libm.so.6 is a symlink pointing to libm-2.13.so.
- Locate libm-2.13.so on your Ubuntu Linux machine. It is usually located at /lib/i386-linux-gnu/libm-2.13.so.
- Copy libm-2.13.so to /Developer/Cocotron/1.0/Linux/i386/gcc-4.3.1/i386-ubuntu-linux/lib.
- Create a symlink libm.so.6 -> libm-2.13.so in that directory.
- Finally, build the command line tool by hitting Cmd+B.
Step 6: Installing and Testing the Command Line Interface on Ubuntu Linux
- Locate Cocotron’s Foundation.framework directory on your Mac (see step 2.2.)
- Copy the complete directory to /usr/lib on your Ubuntu Linux machine recursively. Make sure to preserve symlinks.
- Within your Linux system’s /usr/lib directory, create a symlink named “libFoundation.so” to /usr/lib/Foundation.framework/libFoundation.so.
- 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!
I hope this tutorial was helpful for you, what ever you intend to do with Objective-C on Linux.
Of course, porting OS X based code over to Linux is the most obvious use case here. As outlined in my previous post I see great potential in combining both platforms in a client/server manner. Thanks to the Cocotron project, this is now perfectly possible.
However, there are still a number of things I would love to have:
- a GUI library (not necessarily an exact Cocoa ripoff) on Linux – this is not currently provided by Cocotron,
- the compiler tool chain for building the complete Cocotron frameworks and apps based on that on Linux,
- an IDE for developing Objective-C apps on Linux,
- and, finally, 64 bit support for Objective-C apps on Linux.
If there are others out there who think that these tools are really needed, please don’t hesitate to contact me. I’m willing to invest some of my free time to contribute to one or two of these projects, if there are fellow devs willing to do the same!