Sunday, May 22, 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.
//main.m
#import <foundation/foundation.h>

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

Now type
make
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
Or
gcc -o main main.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString