Saturday, January 21, 2012

Writing an "Hello world" Program in Objective-C Application with Xcode

Xcode will create skeleton versions of the files needed to build a command-line based Objective-C application. Objective-C source files are identified by the .m filename extension. In the case of our example, Xcode has pre-created a main source file named sampleApp.m and populated it with some basic code ready for us to edit. To view the code, selectsampleApp.m from the list of files so that the code appears in the editor area beneath the list. The skeleton code reads as follows:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
    NSLog(@"Hello, World!");
    [pool drain];
    return 0;
}
With the change made, the next step is to compile and run the application by selecting the Build and Run option located in the Xcode Build menu. Once this option has been selected, Xcode will compile the sources and run the application.  
Compiling Objective C program from command line::
gcc -framework Foundation hello.m -o hello
./hello
2009-09-25 15:51:11.528 hello[3371:10b] hello world

No comments:

Post a Comment