Changes

Jump to: navigation, search

Installing Xcode and Compiling Objective-C on Mac OS X

1,436 bytes added, 19:52, 25 September 2009
Compiling Objective-C from the Command Line
== Compiling Objective-C from the Command Line ==
 
While Xcode provides a powerful environment that will prove invaluable for larger scale projects, for compiling a running such as simple application as the one we have been working with in this chapter it is a little bit of overkill. It is also a fact that some developers feel that development environments like Xcode just get in the way and prefer to use a basic editor and command line tools to develop applications. After all, in the days before integrated development environments came into favor, this was how all applications were developed. Whilst I'm not suggesting that everyone abandon Xcode in favor of the ''vi'' editor and GNU compiler, it is useful to know that the option to work form the command line is available.
 
Using your favorite text or programming editor, create a file named ''hello.m'' containing the following Objective-C code:
 
<pre>
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
NSLog (@"hello world");
[pool drain];
return 0;
}
</pre>
 
Save the file and then open a Terminal window (if you aren't already working in one) and compile the application with the following command:
 
<pre>
gcc -framework Foundation hello.m -o hello
</pre>
 
Assuming the code compiles without error it can be run as follows:
 
<pre>
./hello
2009-09-25 15:51:11.528 hello[3371:10b] hello world
</pre>

Navigation menu