Friday, December 21, 2007

objective-c hello world

I just started learning objective-c and cocoa a few days ago, and here is how I wrote an overly complicated objective-c "hello world" application.

MyClass.h:
#import <objc/Object.h>

@interface MyClass : Object {
}

- (void) sayHello;


MyClass.m:
#import "MyClass.h"
#import <stdio.h>
@implementation MyClass : Object {

}

- (void) sayHello { printf("Hello, World!\n"); }


main.m:
#import <objc/objc.h>
#import "MyClass.h"

int main() {
MyClass *anObject;

anObject = [[MyClass alloc] init];
[anObject sayHello];

return 0;
}

I compiled this using the following:
gcc -o hello -lobjc MyClass.m main.m