Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 38 ยท Getting Started

Setting Up and Your First Program

Install an Objective-C toolchain on macOS, Linux or Windows, write a Hello World with Foundation, and learn what each line of the program does.

To write Objective-C you need a compiler that understands the language and a copy of the Foundation framework. How you get them depends on your operating system. This lesson shows the three common setups, walks through a first program line by line, and explains the small ideas (such as the autorelease pool) that show up in every program.

Objective-C code cannot run in your browser here. The code blocks marked objectivec are read-only, and where it helps, a small C stand-in shows the same idea in runnable form.

Choosing a toolchain

macOS (easiest). Install Xcode from the App Store, or only the command line tools with xcode-select --install. You get clang (the compiler) and all Apple frameworks, including Foundation. Create a "Command Line Tool" project in Xcode, or compile from Terminal as shown below.

Linux. Install clang, GNUstep and its base library. On Debian or Ubuntu the packages are named like clang, gnustep, gnustep-devel and libgnustep-base-dev. GNUstep is an open-source implementation of the Foundation APIs, so most examples in this course work unchanged.

Windows. Use WSL (Windows Subsystem for Linux) and follow the Linux steps. It is far less work than a native GNUstep install.

To check that the compiler is present:

Terminal
clang --version

On macOS you should also be able to run xcrun --show-sdk-path. On Linux, gnustep-config --objc-flags prints the flags GNUstep needs.

Hello, World

Create a file named hello.m. The .m extension tells the compiler the file contains Objective-C.

OBJECTIVEC
#import <Foundation/Foundation.h>

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

Compile and run it on macOS:

Terminal
clang -fobjc-arc -framework Foundation hello.m -o hello
./hello

On Linux with GNUstep, the flags come from gnustep-config:

Terminal
clang -fobjc-arc $(gnustep-config --objc-flags) hello.m \
  $(gnustep-config --base-libs) -o hello
./hello

The program prints one line. NSLog adds a timestamp, the process name and its id before your text, so the real output looks like 2025-01-01 10:00:00.123 hello[4242:1337] Hello, World!. The part you wrote is Hello, World!.

The program line by line

#import <Foundation/Foundation.h> pulls in the Foundation framework headers. #import is like #include, but it automatically guarantees a header is included only once. Foundation defines NSObject, NSString, NSArray, NSLog and hundreds of other types.

int main(int argc, const char * argv[]) is the same entry point as in C. The program starts here, and the return value is the exit status. 0 means success.

@autoreleasepool { ... } creates a scope in which temporary objects can be cleaned up when the scope ends. Even with ARC you should keep it in main, and the Foundation templates in Xcode generate it for you. Memory management is covered fully in chapter 5; for now treat it as required boilerplate.

NSLog(@"Hello, World!"); prints a line to standard error. The string literal starts with @: @"..." is an NSString, not a C string. Plain "..." is a C string, and the two are different types.

return 0; reports success to the shell.

A second program with a bit more

OBJECTIVEC
#import <Foundation/Foundation.h>

int main(void) {
    @autoreleasepool {
        NSString *language = @"Objective-C";
        int year = 1984;
        NSLog(@"%@ appeared around %d", language, year);

        NSArray *tools = @[@"clang", @"Foundation", @"Xcode"];
        for (NSString *tool in tools) {
            NSLog(@"Tool: %@", tool);
        }
    }
    return 0;
}

Here %@ prints any Objective-C object by calling its description method, %d prints an int, @[ ... ] creates an NSArray, and for (... in ...) is fast enumeration. Every one of these is explained in later lessons, so read them now only for the overall shape.

Use int main(void) or int main(int argc, const char * argv[]). Both work. The second form is what Xcode generates, and you need it when the program reads command-line arguments.

Stand-in: what NSLog does for you

Because we cannot run Objective-C here, this C program imitates the shape of NSLog: a function that takes a format string, prefixes a label and ends with a newline. It uses a variadic function, a topic that returns in Variadic Functions and Storage Classes.

C Stand-in (C): an NSLog-like function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Real NSLog differs in two ways: it writes to standard error instead of standard output, and %@ is a format code that works only with objects.

Stand-in: the shape of main with a pool

An @autoreleasepool is a block scope with a start and an end. Here is a C model that "drains" the items registered inside the scope when it ends. It is a simplified picture of the real mechanism, not the real runtime.

C Stand-in (C): scope with cleanup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Common problems

  • 'Foundation/Foundation.h' file not found means the compiler cannot see the Foundation headers. On Linux add the gnustep-config --objc-flags output.
  • Linker errors about _OBJC_CLASS_$_NSObject mean you forgot -framework Foundation (macOS) or the GNUstep base libraries (Linux).
  • A file saved as .c is compiled as C, and @autoreleasepool is a syntax error. Use .m.
  • Output appears with a long prefix. That is normal for NSLog. Use printf if you need plain output, as the next lesson shows.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file extension is used for Objective-C implementation files?

    Implementation files use .m (and .mm for Objective-C++). Headers use .h.
  2. 2. What does the @ in @"Hello" tell the compiler?

    @"..." creates an NSString object. A string without @ is a plain C string.
  3. 3. Why does real NSLog output start with a date and process id?

    NSLog is a logging function; the prefix is part of its output format.
  4. 4. Which toolchain implements Foundation on Linux?

    GNUstep is an open-source implementation of Foundation and AppKit for Linux and Windows.

Summary

  • On macOS use Xcode or the command line tools; on Linux and Windows (WSL) use clang plus GNUstep.
  • Objective-C source lives in .m files, and Foundation is imported with #import <Foundation/Foundation.h>.
  • main is the entry point as in C; wrap work in @autoreleasepool.
  • NSLog(@"...") prints with a timestamp; @"..." makes an NSString.
  • Compile with clang -fobjc-arc -framework Foundation file.m on macOS.

Next: Program Structure and NSLog