Friday, January 6, 2012

Compiling and Running Objective-C Code on Windows

My book, Core Objective-C in 24 Hours, includes numerous source code examples that are free to use.  I developed the software using Xcode 4 on my Mac Pro, but thought that it would be a good idea to provide instructions on compiling and running this code (and Objective-C code in general) on Windows machines.  Here are the steps:
  1. Download and install GNUstep for Windows.  The GNUstep MSYS System and GNUstep Core packages are required to run GNUstep and use the core libraries.  An installer is provided for each of these packages; simply launch each installer and follow the step-by-step instructions. GNUstep is a free (GNU LGPL), open-source version of the Cocoa framework.  It includes an implementation of the AppKit and Foundation libraries as well as development tools used with Cocoa, such as Gorm and ProjectCenter (Xcode).  More information on GNUstep can be found here.
  2. Start the GNUstep shell from the Windows start menu.  The shell prompt will appear as follows:

    <username>@hostname ~
    $

  3. Map the home directory for the GNUstep environment to the corresponding location on the Windows file system.  Depending on the installation location you chose, this directory may map to something like C:\GNUstep\home\<username> or C:\GNUstep\msys\1.0\home\<username>.
  4. Create your Objective-C program source code under the GNUstep home directory using your editor of choice.

  5. Compile your Objective-C code using the gcc compiler.  In the following example, the code consists of 3 files:  main.m, hello.h, hello.m, and the program is named hello:

    <username>@hostname ~
    $ gcc `gnustep-config –-objc-flags` -o hello main.m hello.m –L /GNUstep/System/Library/Libraries –lobjc –lgnustep-base


    The `gnustep-config --objc-flags` selection instructs gcc to run the gnustep-config script to configure the GNUstep environment, printing out all the flags required to compile an Objective-C file (the --objc-flags option).  The –L /GNUstep/System/Library/Libraries option adds this directory to the list of directories searched when using the –l option.  The –lobjc and –lgnustep-base options link the objc and gnustep-base libraries (both located in the /GNUstep/System/Library/Libraries directory) to create the executable program.
     
  6. After the program has been compiled with no errors, run it!

    <username>@hostname ~
    $ ./hello.exe

    That’s all there is to it, you can now compile and run Objective-C programs on Windows!  

No comments:

Post a Comment