Objective-C for C/C++ Programmers

Here is a very good tutorial: http://www.otierney.net/objective-c.html, it shows how to use Objective-C in Mac OS X, Linux and Cygwin/Windows. This post complements the tutorial with some details.

To programming using Objective-C on Mac OS X, we need XCode, which contains the Objective-C compiler that we need. On Linux or Cygwin, we need to install GNU Compiler Collection (GCC) in order to get the compiler.

However, programming in bare-bone Objective-C is not funny. Instead, we want a versatile toolkit which helps us with UTF-8 strings, memory management, parallel computing, GUI and etc. This is like how C++ programmers rely on STL (and maybe boost) and how Java developers rely on JDK.

On Mac OS X, this versatile toolkit is Cocoa, which is installed as part of XCode. On Linux and Cygwin, we have the sibling of Cocoa, GNUStep. Both Cocoa and GNUstep are descendants of OpenStep, a product created by NeXTSTEP and Sun jointly.

XCode can be downloaded from Apple’s developer program, and the installation process is straight-forward. On Ubuntu/Debian Linux, we can install GNUstep from binary package:

sudo apt-get install gnustep-devel

On Cygwin, we have to build and install GNUstep from source package. Here is a step-by-step guide.

Given Objective-C compilers and Cocoa/GNUstep installed, we can start working on a hello world program:

#import <stdio.h>
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]) {
  printf("Hello World!\n");
  NSLog(@"Hello World!\n");
  return 0;

To build this program on Mac OS X (using GCC distributed with XCode):

gcc main.m -framework Foundation -o hello

or, using Clang instead of GCC:

clang main.m -framework Foundation -o hello

Where, the term “framework” comes from NextSTEP and indicates a package of header files and libraries.

To build the program on Linux:

gcc -o hello main.m -I/usr/include/GNUstep -lgnustep-base -lobjc

You can checkout above source code and Makefile from the following SVN repository:

svn checkout http://yiwang-learning-projects.googlecode.com/svn/trunk/objc-runtime

For a concise description about the Objective-C language, please refer to: http://cocoadevcentral.com/d/learn_objectivec, or the official document.