Compiling Objective-C without XCode in OS X
Ok, so I’m about to blog about something which may seem blindingly obvious to OS X developers, but to be fair I didn’t find much on Google myself.
I’m new to Objective-C and programming for OS X in general, but I get frustrated when all articles, books and tutorials narrow themselves down to just one IDE and push you away from the glory of the command line. So how do you compile a simple “Hello World!” application in objective-C on a Mac?
This is a brief “Hello World!” tutorial for ObjC, on top of demonstrating the build process beyond XCode.
We’ll create a class called “Talker” which simply says things we tell it to, then we’ll run it from our main() function.
Create your directory structure
Simple enough. Make yourself a directory called “HelloWorld” or something like that, then create a sub-directory called “build”.
mkdir HelloWorld/build
Create the interface and implementation files
HelloWorld/Talker.h
HelloWorld/Talker.m
@implementation Talker
- (void) say: (STR) phrase {
printf("%s\n", phrase);
}
@end
Create your main() file
HelloWorld/hello.m
int main(void) {
Talker *talker = [[Talker alloc] init];
[talker say: "Hello World!"];
[talker release];
}
Compile it with gcc specifying -framework Foundation
Run it!
…
Hello World!
Enjoy :)
WordPress appears to have removed some formatting whitespace from my code, but it will still compile.
No Responses to “Compiling Objective-C without XCode in OS X”
No comments yet