Objective C programming in Windows – GNUStep & ProjectCenter
I have been toying with the idea of getting into full fledged iPhone application development for sometime now. To get into iPhone development, you need to be familiar with Objective C language and Cocoa framework. But the iPhone development environment is only available for Mac platform (which includes Xcode IDE and iPhone simulator). If you want to run iPhone development environment without a Mac machine, you have two options,
- Use a virtual machine platform like VMWare to run OSX under Windows – However it is not legal to run OSX under non Apple hardware.
- Another option is to use OSx86 which allows you to install Mac OS on top of your Intel or AMD computer.
But if you just want to learn Objective C language or Cocoa framework programming, you don’t need any of the above. You just need the GNUStep and ProjectCenter tools. This article explains how GNUStep can be configured as a Objective C development environment on Windows.
Objective C Programming in Windows using GNUStep
GNUStep is a free, object oriented development environment with built in tools such as a compiler. The C/C++ compiler included (gcc) also supports compiling Objective C programs. It also has a graphical development kit with API similar to Cocoa framework (since both of them were derived from the old OpenStep framework). So installing GNUStep on Windows gives an instant development environment for Objective C programs. An extension to GNUStep is the ProjectCenter (Xcode equivalent) using which graphical programs can be built.
Installing GNUStep on Windows
GNUStep is available as a Windows installer from the official page. I recommend downloading and installing GNUStep System, GNUStep Core and GNUStep Devel. Note that the latest version available as of October 2009 is 0.23.0 and this version is not compatible with ProjectCenter 0.50. If you are planning to use ProjectCenter I recommend that you download 0.22.0 version of GNUStep from the download page. Here is a summary of what is required,
- Objective C programming only – GNUStep 0.23 (System | Core | Devel)
- Objective C Programming and ProjectCenter 0.50 - GNUStep 0.22 (System | Core | Devel)
You can download pre-compiled version of ProjectCenter 0.50 for Windows from here.
Once you install all the binaries above, you will have GNUStep shell under Programs->GNUStep (See figure). Click on the Shell to invoke the command line interface. This shell is based on MinGW (collection of gcc compiler and command line tools) and using it you can compile and run Objective C programs. The command line is similar to Unix/Linux command line and you can navigate to any folder in your windows machine. Using the built in gcc program you can compile and run Objective C programs.
Compiling and running Objective C programs in GNUStep on Windows
Create the following program and save it with the name helloworld.m using notepad,
1 2 3 4 5 6 7 8 | #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSLog (@"Hello World!"); [pool drain]; return 0; } |
Now using GNStep shell navigate to the folder where helloworld.m is stored (in my case it is cd w:/prg). Type in the following command to compile helloworld.m.
gcc -o helloworld helloworld.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString
Please note that the various switch options such as -lobjc should appear at the end of the command. the -o switch specifies the name of the executable created (helloworld.exe) in this case. Following is the result of compilation,
Info: resolving ___objc_class_name_NSAutoreleasePool by linking to __imp____objc
_class_name_NSAutoreleasePool (auto-import)
Info: resolving ___objc_class_name_NSConstantString by linking to __imp____objc_
class_name_NSConstantString (auto-import)
W:\Tools\GNUstep\mingw\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.e
xe: warning: auto-importing has been activated without –enable-auto-import spec
ified on the command line.
This should work unless it involves constant data structures referencing symbols
from auto-imported DLLs.
You can see that helloworld.exe is generated in the same folder (Ignore compiler output and warnings or use -enable-auto-import switch to fix them). To run the program type in ./helloworld.exe at the command prompt,
Common Errors during Objective C compilation
- error: cannot find interface declaration for `NXConstantString’ – This means that you haven’t added the switch -fconstant-string-class=NSConstantString to the gcc command line.
- Foundation/Foundation.h: No such file or directory – This means that gcc is unable to find Foundation header classes. Use the switches -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries
- stray ‘@’ in program – This means that you have the wrong double quote in your source code. Use " instead of ”.
Now you are all set for Objective C programming and a bit of Cocoa programming on Windows. Good luck! and buy a Mac machine when you are ready to write iPhone programs.
Further Reference
October 23, 2009 | Posted in Programming 16 Comments » | By Jayson
March 16th, 2011 at 9:08 pm
Now using GNStep shell navigate to the folder where helloworld.m is stored (in my case it is cd w:/prg). Type in the following command to compile helloworld.m.
How do I do this?
March 25th, 2011 at 2:37 am
thanks very helpful!!
$ gcc -o progname prog.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString
is this the command that you would use to compile all objc programs on windows??
March 26th, 2011 at 9:02 pm
the following shows up when I try to run the first program
c:/helloworld.m line 2: syntax error near unexpected token ‘(‘
c:/helloworld.m line 2 ‘int main (int argc, const char * argv [])’
April 20th, 2011 at 1:14 am
Michael, you use UNIX operations. If you don’t know how, to simplify it, save helloworld.m into C:/GNUStep. Then run the GNUStep Shell, and keep typing “cd ..” (without quotations) until you see nothing change. Then you can compile.
Which is the step I’m having trouble with, I’m getting errors compiling: “Cannot find -lojbc. Anyone have any idea?
June 9th, 2011 at 4:20 am
I got following errors
<-lobjc -lgnustep-base -fconstant-string-class=NSConstantString
helloworld.m:1:35: error: Foundation/Foundation.h: No such file or directory
helloworld.m: In function 'main':
helloworld.m:4: error: 'NSAutoreleasePool' undeclared (first use in this functio
n)
helloworld.m:4: error: (Each undeclared identifier is reported only once
helloworld.m:4: error: for each function it appears in.)
helloworld.m:4: error: 'pool' undeclared (first use in this function)
helloworld.m:5: error: cannot find interface declaration for 'NSConstantString'
let me know whats wrong…?
December 1st, 2011 at 1:48 am
Hi !
I have the next error:
The current setting for native-objc-exceptions does not match that of gnustep-base
How I can fix this error?
Thank You !
January 30th, 2012 at 12:19 pm
I’ve installed:
1. gnustep-msys-system-0.29.0-setup.exe
2. gnustep-core-0.29.0-setup.exe
3. gnustep-devel-1.4.0-setup.exe
on WinXP SP3.
I tried to run the above Obj C example, but I get this error:
$ gcc -o helloworld helloworld.m -I /GNUstep/System/Library/Headers -L /GNUstep
/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConst
antString
In file included from C:/GNUstep/GNUstep/System/Library/Headers/Foundation/NSCla
ssDescription.h:30:0,
from C:/GNUstep/GNUstep/System/Library/Headers/Foundation/Found
ation.h:50,
from helloworld.m:1:
C:/GNUstep/GNUstep/System/Library/Headers/Foundation/NSException.h:44:2: error:
#error The current setting for native-objc-exceptions does not match that of gnu
step-base … please correct this.
I can compile and run a C program correctly from the same folder. Any idea how to solve it? Thanks.
January 30th, 2012 at 2:49 pm
OK, I fixed the path like this:
/GNUstep/GNUstep/System/Library/Headers
and
/GNUstep/GNUstep/System/Library/Libraries
but now I get
helloworld.m:1:23: fatal error: Foundation/Foundation.h: No such file or directory
compilation terminated.
I checked and Foundation.h is in
/GNUstep/GNUstep/System/Library/Headers/Foundation
Am I missing something basic here?
January 30th, 2012 at 7:34 pm
OK, it works now.
The solution (for me) is:
gcc `gnustep-config –objc-flags` -o hello2 hello2.m -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base
Strange, but nice?
February 1st, 2012 at 3:56 am
Thank you so much – it worked!!! Could you show me how to compile in ProjectCenter?
February 9th, 2012 at 8:33 am
I tried it with latest versions (0.29.0) of GNUStep.
First install the gnustep-msys-system package, gnustep-core, then gnustep-devel package.
try gcc compile it gives me following error.
/GNUstep/GNUstep/System/Library/Headers/Foundation/NSException.h:44:2: error:
#error The current setting for native-objc-exceptions does not match that of gnu
step-base … please correct this.
Fix:
change :\GNUstep\GNUstep\System\Library\Headers\GNUstepBase\GSConfig.h :
#define BASE_NATIVE_OBJC_EXCEPTIONS 0
then the compile works
February 13th, 2012 at 12:58 pm
I got same problem—->
<-lobjc -lgnustep-base -fconstant-string-class=NSConstantString
helloworld.m:1:35: error: Foundation/Foundation.h: No such file or directory
helloworld.m: In function 'main':
helloworld.m:4: error: 'NSAutoreleasePool' undeclared (first use in this functio
n)
helloworld.m:4: error: (Each undeclared identifier is reported only once
helloworld.m:4: error: for each function it appears in.)
helloworld.m:4: error: 'pool' undeclared (first use in this function)
helloworld.m:5: error: cannot find interface declaration for 'NSConstantString'
plz let me know whats wrong…?
February 17th, 2012 at 9:30 am
I run the code successfull as follows the author’s steps, But I copy the execution command into the Windows command window.
Another different is , the .m file location, if put different folder, user need to get to that folder in the command window.
March 14th, 2012 at 12:16 pm
i get following error.
helloworld.m:1:35: fatal error: Foundation/Foundation.h: No such file or directory
compilation terminated.
how to solve this plz.
April 19th, 2012 at 5:48 pm
[…] more info click here Share this:TwitterFacebookLike this:LikeBe the first to like this […]
August 16th, 2012 at 7:37 am
i’ve got this error:
H:\GNUstep\mingw\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot find -lobjc
collect2: lb returned 1 exit status
help to solve this please…