Sunday, 22 May 2011

Compile Objective-C in Windows

Objective-C programs can be compiled under Windows using the GNUStep mingw gcc compiler with the necessary libraries.

The easiest way to compile is to write a GNUmakefile.
Let's say that you have a HelloWorld Project that has a main.m file.
#import <foundation/foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]
    NSLog(@"Hello World");
    [pool drain];
    return 0;
include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME = HelloWorld
HelloWorld_OBJC_FILES = main.m
include $(GNUSTEP_MAKEFILES)/tool.make

Now type
inside the directory and you'll get a directory called obj were you have the executable.

Alternatively, you can use any of the following commands:
gcc `gnustep-config --objc-flags` -o main main.m -L /GNUStep/System/Library/Libraries -lobjc -lgnustep-base
gcc -o main main.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString