Objective C programming in Windows – GNUStep & ProjectCenter

I have been toying with the idea of getting into full fledged iPhone application development for sometime now. To get into iPhone development, you need to be familiar with Objective C language and Cocoa framework. But the iPhone development environment is only available for Mac platform (which includes Xcode IDE and iPhone simulator). If you want to run iPhone development environment without a Mac machine, you have two options,

  • Use a virtual machine platform like VMWare to run OSX under Windows – However it is not legal to run OSX under non Apple hardware.
  • Another option is to use OSx86 which allows you to install Mac OS on top of your Intel or AMD computer.

But if you just want to learn Objective C language or Cocoa framework programming, you don’t need any of the above. You just need the GNUStep and ProjectCenter tools. This article explains how GNUStep can be configured as a Objective C development environment on Windows.

Objective C Programming in Windows using GNUStep

GNUStep is a free, object oriented development environment with built in tools such as a compiler. The C/C++ compiler included (gcc) also supports compiling Objective C programs. It also has a graphical development kit with API similar to Cocoa framework (since both of them were derived from the old OpenStep framework). So installing GNUStep on Windows gives an instant development environment for Objective C programs. An extension to GNUStep is the ProjectCenter (Xcode equivalent) using which graphical programs can be built.

Installing GNUStep on Windows

GNUStep is available as a Windows installer from the official page.  I recommend downloading and installing GNUStep System, GNUStep Core and GNUStep Devel. Note that the latest version available as of October 2009 is 0.23.0 and this version is not compatible with ProjectCenter 0.50.  If you are planning to use ProjectCenter I recommend that you download 0.22.0 version of GNUStep from the download page. Here is a summary of what is required,

  • Objective C programming only – GNUStep 0.23 (System | Core | Devel)
  • Objective C Programming and ProjectCenter 0.50 -  GNUStep 0.22 (System | Core | Devel)

You can download pre-compiled version of ProjectCenter 0.50 for Windows from here.

image Once you install all the binaries above, you will have GNUStep shell under Programs->GNUStep (See figure). Click on the Shell to invoke the command line interface. This shell is based on MinGW (collection of gcc compiler and command line tools) and using it you can compile and run Objective C programs. The command line is similar to Unix/Linux command line and you can navigate to any folder in your windows machine. Using the built in gcc program you can compile and run Objective C programs.

Compiling and running Objective C programs in GNUStep on Windows

Create the following program and save it with the name helloworld.m using notepad,

1
2
3
4
5
6
7
8
#import <Foundation/Foundation.h> 
int main (int argc, const char * argv[])
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  NSLog (@"Hello World!");
  [pool drain];
  return 0;
}

Now using GNStep shell navigate to the folder where helloworld.m is stored (in my case it is cd w:/prg). Type in the following command to compile helloworld.m.

gcc -o helloworld helloworld.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString

Please note that the various switch options such as -lobjc should appear at the end of the command. the -o switch specifies the name of the executable created (helloworld.exe) in this case. Following is the result of compilation,

Info: resolving ___objc_class_name_NSAutoreleasePool by linking to __imp____objc

_class_name_NSAutoreleasePool (auto-import)

Info: resolving ___objc_class_name_NSConstantString by linking to __imp____objc_

class_name_NSConstantString (auto-import)

W:\Tools\GNUstep\mingw\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.e

xe: warning: auto-importing has been activated without –enable-auto-import spec

ified on the command line.

This should work unless it involves constant data structures referencing symbols

from auto-imported DLLs.

You can see that helloworld.exe is generated in the same folder (Ignore compiler output and warnings or use -enable-auto-import switch to fix them). To run the program type in ./helloworld.exe at the command prompt,

image

Common Errors during Objective C compilation

  • error: cannot find interface declaration for `NXConstantString’ – This means that you haven’t added the switch -fconstant-string-class=NSConstantString to the gcc command line.
  • Foundation/Foundation.h: No such file or directory – This means that gcc is unable to find Foundation header classes. Use the switches -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries
  • stray ‘@’ in program – This means that you have the wrong double quote in your source code. Use " instead of .

Now you are all set for Objective C programming and a bit of Cocoa programming on Windows. Good luck! and buy a Mac machine when you are ready to write iPhone programs.

Further Reference

Click here to get latest site updates delivered to your email. You need to confirm subscription by clicking on the link sent by feedburner to your email address.


October 23, 2009 | Posted in Programming 16 Comments » | By Jayson Joseph

16 Comments to “Objective C programming in Windows – GNUStep & ProjectCenter”

  1. Michael Says:

    Now using GNStep shell navigate to the folder where helloworld.m is stored (in my case it is cd w:/prg). Type in the following command to compile helloworld.m.

    How do I do this?

  2. Rob Says:

    thanks very helpful!!

    $ gcc -o progname prog.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString

    is this the command that you would use to compile all objc programs on windows??

  3. Michael Says:

    the following shows up when I try to run the first program

    c:/helloworld.m line 2: syntax error near unexpected token ‘(‘
    c:/helloworld.m line 2 ‘int main (int argc, const char * argv [])’

  4. Zero Says:

    Michael, you use UNIX operations. If you don’t know how, to simplify it, save helloworld.m into C:/GNUStep. Then run the GNUStep Shell, and keep typing “cd ..” (without quotations) until you see nothing change. Then you can compile.

    Which is the step I’m having trouble with, I’m getting errors compiling: “Cannot find -lojbc. Anyone have any idea?

  5. monu Says:

    I got following errors

    <-lobjc -lgnustep-base -fconstant-string-class=NSConstantString
    helloworld.m:1:35: error: Foundation/Foundation.h: No such file or directory
    helloworld.m: In function 'main':
    helloworld.m:4: error: 'NSAutoreleasePool' undeclared (first use in this functio
    n)
    helloworld.m:4: error: (Each undeclared identifier is reported only once
    helloworld.m:4: error: for each function it appears in.)
    helloworld.m:4: error: 'pool' undeclared (first use in this function)
    helloworld.m:5: error: cannot find interface declaration for 'NSConstantString'

    let me know whats wrong…?

  6. ARTURO OLMEDO Says:

    Hi !
    I have the next error:
    The current setting for native-objc-exceptions does not match that of gnustep-base

    How I can fix this error?

    Thank You !

  7. AJ Says:

    I’ve installed:

    1. gnustep-msys-system-0.29.0-setup.exe
    2. gnustep-core-0.29.0-setup.exe
    3. gnustep-devel-1.4.0-setup.exe

    on WinXP SP3.

    I tried to run the above Obj C example, but I get this error:

    $ gcc -o helloworld helloworld.m -I /GNUstep/System/Library/Headers -L /GNUstep
    /System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConst
    antString
    In file included from C:/GNUstep/GNUstep/System/Library/Headers/Foundation/NSCla
    ssDescription.h:30:0,
    from C:/GNUstep/GNUstep/System/Library/Headers/Foundation/Found
    ation.h:50,
    from helloworld.m:1:
    C:/GNUstep/GNUstep/System/Library/Headers/Foundation/NSException.h:44:2: error:
    #error The current setting for native-objc-exceptions does not match that of gnu
    step-base … please correct this.

    I can compile and run a C program correctly from the same folder. Any idea how to solve it? Thanks.

  8. AJ Says:

    OK, I fixed the path like this:
    /GNUstep/GNUstep/System/Library/Headers

    and
    /GNUstep/GNUstep/System/Library/Libraries

    but now I get
    helloworld.m:1:23: fatal error: Foundation/Foundation.h: No such file or directory
    compilation terminated.

    I checked and Foundation.h is in
    /GNUstep/GNUstep/System/Library/Headers/Foundation

    Am I missing something basic here?

  9. AJ Says:

    OK, it works now.

    The solution (for me) is:
    gcc `gnustep-config –objc-flags` -o hello2 hello2.m -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base

    Strange, but nice?

  10. Bill Kahng Says:

    Thank you so much – it worked!!! Could you show me how to compile in ProjectCenter?

  11. Yawar Khan Says:

    I tried it with latest versions (0.29.0) of GNUStep.

    First install the gnustep-msys-system package, gnustep-core, then gnustep-devel package.

    try gcc compile it gives me following error.
    /GNUstep/GNUstep/System/Library/Headers/Foundation/NSException.h:44:2: error:
    #error The current setting for native-objc-exceptions does not match that of gnu
    step-base … please correct this.

    Fix:
    change :\GNUstep\GNUstep\System\Library\Headers\GNUstepBase\GSConfig.h :

    #define BASE_NATIVE_OBJC_EXCEPTIONS     0
     
    then the compile works

  12. subha Says:

    I got same problem—->
    <-lobjc -lgnustep-base -fconstant-string-class=NSConstantString
    helloworld.m:1:35: error: Foundation/Foundation.h: No such file or directory
    helloworld.m: In function 'main':
    helloworld.m:4: error: 'NSAutoreleasePool' undeclared (first use in this functio
    n)
    helloworld.m:4: error: (Each undeclared identifier is reported only once
    helloworld.m:4: error: for each function it appears in.)
    helloworld.m:4: error: 'pool' undeclared (first use in this function)
    helloworld.m:5: error: cannot find interface declaration for 'NSConstantString'

    plz let me know whats wrong…?

  13. Zan Way Says:

    I run the code successfull as follows the author’s steps, But I copy the execution command into the Windows command window.
    Another different is , the .m file location, if put different folder, user need to get to that folder in the command window.

  14. Prasanna Says:

    i get following error.

    helloworld.m:1:35: fatal error: Foundation/Foundation.h: No such file or directory
    compilation terminated.

    how to solve this plz.

  15. Running Objective C on Windows | malkitsinghonline Says:

    […] more info click here Share this:TwitterFacebookLike this:LikeBe the first to like this […]

  16. cup Says:

    i’ve got this error:
    H:\GNUstep\mingw\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot find -lobjc
    collect2: lb returned 1 exit status

    help to solve this please…

Leave a Comment