Sunday 10 April 2011

Programming objective-C/Cocoa frameworks on Windows 1 - Install GNUStep and Hello World

I have started exploring the possibility of programming objective-c with Cocoa frameworks on Windows over the weekend.

Objective-C is a super set of C. All native C code can be compiled down by an objective-c compiler. If I only want to play with objective-C the programming language without the Cocoa frameworks provided by Apple, a standard gcc compiler is enough.

To program in objective-C and play with the Cocoa frameworks on my Windows 7, I need to install GNUStep for Windows. GNUStep not only provides an objective-C compiler but also a robust implementation of the foundation, AppKit, and UIKit libraries in the Cocoa frameworks. Developers can program with Cocoa frameworks on many platforms using GNUStep. It is a good tool to give the apple a bite.

To get started, download the GNUStep windows installers and install them in the following order:
  • gnustep-msys-system-0.22.1-setup.exe
  • gnustep-core-0.22.0-setup.exe
  • gnustep-devel-1.0.0-setup.exe
  • ProjectCenter-0.5.0-setup.exe
After installation:


Click Shell and a MinGW window starts:


I can't wait to say "Hello world!".

Start NotePad++, create a Helloworld.m file.

#import <foundation foundation.h>
int main (void)
{
    NSLog(@"Hello world!");
    return 0;
}

Create a GNUMake file in the same directory.

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = HelloWorld
HelloWorld_OBJC_FILES = helloworld.m

include $(GNUSTEP_MAKEFILES)/tool.make

From the shell window, navigate to my source file folder and type "make". My first application is built.



After the "make" commad, a sub folder "obj" has been created with the following items:



To run the Helloworld.exe, in the shell window, type "obj/Helloworld" and you'll see the "Hello world!" greeting in the shell window.

The above is not a very exciting example. How about changing the code and play with AppKit a bit:

#import <foundation foundation.h>
#import <appkit.h>
int main (void)
{
  NSAutoreleasePool *pool = [NSAutoreleasePool new];
  [NSApplication sharedApplication];
  NSRunAlertPanel (@"Test", @"Hello world!", nil, nil, nil);
  [pool drain];
  return 0;
}

The GNUMake file will also need to be modified:

include $(GNUSTEP_MAKEFILES)/common.make

APP_NAME = HelloWorld
HelloWorld_OBJC_FILES = helloworld.m

include $(GNUSTEP_MAKEFILES)/application.make

In the shell window, type "make clean" to cleanup the previous make, then "make".
To run the application, type "OpenApp Helloworld", and a message box will pop up:



Now I have a development environment and a running application - good progress!

10 comments:

Uvamuki said...

I've done everything told below:
Start NotePad++, create a Helloworld.m file.

#import
int main (void)
{
NSLog(@"Hello world!");
return 0;
}

Create a GNUMake file in the same directory.

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = HelloWorld
HelloWorld_OBJC_FILES = helloworld.m

include $(GNUSTEP_MAKEFILES)/tool.make

with copying reduced codes to said appropriate files and it doesn't work.it indicates the following errors:
Helloworld.m: line 2: syntax error near unexpected token '('
Helloworld.m: line 2: 'int main(void)'

Maybe I should have used either "int" or "void", as in C++?but that doesn't explain the first mistake

Yan (Pamela) Yang said...

Hi Uvamuki, not sure if it's a typo, it seemed your #import statement is not completed. It should include foundation.h:
#import

Yan (Pamela) Yang said...

Oh, I realised the tages could not be displayed in the comment.

Yan (Pamela) Yang said...

It sounds like GNUStep not installed properly.

Unknown said...

Hi Pamela, may I know the extension of the GNUMake file?. And why do you want it in the first place.

Yan (Pamela) Yang said...

Hi Chamberlain, the make file does not have an extension.

Anonymous said...

Greetings, the GUI message box works just great. I'm a windows 7 user with GNUstep and codeblocks. I desire to develop a GUI app in objective-c with menus, buttons, textboxes and more. Can you please provide an example of this? If possible, can the example be one that does not utilize a makefile? If a makefile is necessary, that's fine, and thanks.

Nabil Mughal said...

Hello ! Uvamuki
i also stuck to that point where you were on 7 May 2012..........
Errors:
Helloworld.m: line 2: syntax error near unexpected token '('
Helloworld.m: line 2: 'int main(void)'
Plz tell me how you solved these errors ????

Anonymous said...

http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/?C=M;O=D These exe versions have been changed. with latest it does not work. it gives an error missing dll.

Anonymous said...

correct way is here http://www.gnu.org/software/gnustep/experience/Windows.html

Package Required? Stable Unstable Notes
GNUstep MSYS System Required 0.30.0 - MSYS/MinGW System
GNUstep Core Required 0.30.0 0.31.0 GNUstep Core
GNUstep Devel Optional 1.4.0 - Developer Tools
GNUstep Cairo Optional - 0.31.0 Cairo Backend