Hello World in Objective-C

Our first program will print the classic “hello world” message. Here’s the full source code.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"hello world");
    }
    return 0;
}

To run the program, follow these steps:

  1. Save the code in a file named hello_world.m.

  2. Use the clang compiler to compile the code and then execute it.

$ clang -fobjc-arc -framework Foundation hello_world.m -o hello_world
$ ./hello_world
hello world

Sometimes we’ll want to build our programs into binaries that can be executed directly. The steps mentioned above already result in a binary that can be executed directly.

Now that we can run and build basic Objective-C programs, let’s learn more about the language.