Objective-C on Windows Tutorial

This is a explanation of how to compile an Objective-C hello world program on the Windows platform. To follow this tutorial you will need cygwin installed with gcc.

Let’s get started.

First create the header file Hello.h

#import <objc/Object.h>
 
@interface Hello : Object {
 
}
 
-(void)world;
 
@end

Because this is a pure Objective-C program and does not use Apple’s cocoa framework the Hello object extends Object instead of NSObject.

Now create the implementation file Hello.m.

#import "Hello.h"
 
@implementation Hello
 
-(void)world {
  printf("Hello World!\n");
}
@end

Nothing unusual in the Hello.m file. Let’s do some compiling.

gcc -c -Wno-import Hello.m

GCC can compile and link programs so we pass the -c flag to tell it to compile, and it happily creates a sparkling Hello.o object file for us.

The second flag is much more interesting. It specifies not to display warnings if #import statements are used. The free software movement fanatics don’t like #import and it is listed as an obsolete feature[2]. Of course, it has probably been listed as obsolete for twenty years, and if it is ever removed Apple will just fork GCC. Oh wait, Apple is now providing llvm as an option with Xcode so that problem has already been solved.

Now let’s use our object in a program. Create a main.m file.

#import <objc/Object.h>
#import "Hello.h"
 
main () {
 
  id hello;
 
  hello = [Hello new];
  [hello world];
  [hello free];
}

New and free? Where’s alloc, init, retain and release? Those are all defined in NSObject of the Cocoa framework and are not part of the core Objective-C language.

Now compile this new implementation file to create a second object file main.o.

gcc -c -Wno-import main.m

Take the two object files and squish them together with the GCC linker to create a windows executable.

gcc -o helloWorld Hello.o main.o -lobjc

This is simple stuff. The -o parameter specifies the name of the executable. Then we give a list of the object files we created and specify to link to the Objective-C library (-lobjc). Now you should have helloWorld.exe on disk.

The -mno-cygwin Option

You’re really proud of your helloWorld.exe and you think you might have something here. You want to sell your program. Who wouldn’t pay for an application that displays “Hello World”? It makes that computer seem so friendly.

Well you might have a problem. You see you built your program in cygwin and the EXE got linked to a DLL file called cygwin1.dll. If you link and distribute this DLL file then your program falls under the GPL license agreement.

If the following command displays cygwin1.dll then you have a GPL problem.

objdump -p helloWorld.exe | grep “DLL Name”

If you prefer a GUI Dependency Walker is a free tool that can also show you what DLL’s an executable is linked to.

By adding the -mno-cygwin option to the link stage we can create a helloWorld.exe that we can sell without the constraints of the GPL.

gcc -o helloWorld -mno-cygwin Hello.o main.o -lobjc

objdump -p helloWorld.exe | grep “DLL Name”

Kode on.

References

1. Indiana University – CS304 – Compiling Objective-C http://www.cs.indiana.edu/classes/c304/ObjCompile.html

2. The C Preprocessor for gcc version 3(Section 11.3.2)
http://gcc.gnu.org/onlinedocs/gcc-3.4.6/cpp.pdf

3. -mno-cygwin — Building Mingw executables using Cygwin http://www.delorie.com/howto/cygwin/mno-cygwin-howto.html

Comments are closed.