Wednesday, September 5, 2012

objective c compiler for windows

We can install the Objective-C compiler on windows platform using GNUstep. GNUstep is a free and open version of cocoa API's and tools for different platforms. GNUstep environment can be made in windows using a toolkit MinGW. MinGW stands for Minimal GNU for Windows. And it is a GNU compiler collection which includes Ojective-C compiler as well. GNUstep package itself contains MinGW package, so there is no need to install MinGW package.

Installation of GNUstep: 
  • Download the windows installation package which includes three System, Core and Dev exe files from official website.
  • Install the packages System, Core and Dev in order.
And thats it, you have done the GNUstep environment in your system. To run the Objective-C program/application, go to Programs->GNUStep and there you can find Shell, just click on Shell you will get the Comman Line Interface. This shell is based on MinGW package. So you can able to compile the Objective-C applications/programs from this shell.

Compiling and running Objective C program on Windows: Create the HelloWorld program with below code in any text editor.
#import <Foundation/Foundation.h> 
int main (int argc, const char * argv[])
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  NSLog (@"Hello World!");
  [pool drain];
  return 0;
}
save the file as hello.m and go to the shell and the compile the file with following command.
gcc `gnustep-config --objc-flags` -L /GNUstep/System/Library/Libraries 
hello.m -o hello -lgnustep-base -lobjc
If every thing goes fine, it will generate hello.exe file, because we specified the object file name as hello. If we are not using -o option, it will create a.exe file which is similar to a.out file. To execute the program, run the below command from the shell.
$ ./hello.exe
2012-09-05 11:57:49.615 hello[1168] Hello World!

Click here for what are the possible errors with gcc for objective-C and  why.

Popular Posts