Skip to content

May 13, 2012

Quick Guide Objective C and GNUStep on Windows

by noise

If you want to learn Objective C in Windows there’s a nice library for that called GNUStep. GNUStep uses Cygwin platform.

So in order to compile Objective C apps under windows we will first install: GNUStep MSYS System, GNUstep Core and GNUstep Devel which can be downloaded from here: gnustep.org .

To edit our sourcecode we will install a nice text editor called notepad++.

After installation process we will create a source file with a simple example, that we will save in C:\GNUstep\msys\1.0\home\john\example1.m (assuming john is your windows username):

#import <Foundation/Foundation.h>
 
int main( int argc, const char *argv[] ) {
 
    NSLog (@"First ObjC Program");
 
    return 0;
}

Now we will run GNUstep command line shell and we will compile our code. To run the shell click Start -> Programs -> GNUstep -> Shell.

Then you should see the source code file we just created (example1.m), because if you type pwd (print workind directory shell command) you are in /home/john which is in fact C:\GNUstep\msys\1.0\home\john\ (the real Windows path).

To compile the sourcecode we will run the following command in our GNUStep shell:

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

To run the file we will type example1.exe from command line, since this is a command line app.

Now let’s create a simple GUI app. In order to do that, create a file called example2.m and save it in the same path as previous example:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h rel="nofollow">
 
int main (void)
{
  NSAutoreleasePool *pool;
 
  pool = [[NSAutoreleasePool alloc] init];
 
  [NSApplication sharedApplication];
 
  NSRunAlertPanel (@"GUI App", @"GUI App using GNUStep in Windows",
                   @"Okay", nil, nil);
 
  [pool drain];
 
  return 0;
}

To compile this example we will run from GNUStep command line:

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

To run this file we will double click on example2.exe.

To compile example2.m and disable console window that appears when you run example2.exe use -mwindows paramerer:

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

Note: If you run example2.exe from GNUstep command line, just run ./example2.exe and not example2.exe, because GNUstep shell is ported from Linux where you run binaries that are located in current dir by adding ./ in front of the file name.

Read more from Misc Noise

Leave a Reply

required
required

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments