Skip to main content

Hello World (first programs) · Beginner · 15 min

Hello World in Objective-C with clang on macOS or GNUstep on Linux

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · GCC 15.2 (gobjc) · GNUstep Base 1.31.1

You will compile and run an Objective-C Hello World with GCC and GNUstep on Ubuntu 26.04 (or clang on a Mac), then split a small Greeter class into a header and an implementation and send it your first message.

On this page

Objective-C is the language most of Apple's own frameworks were written in, and the language iPhone apps were written in from 2008 until Swift arrived in 2014. Apple's advice today is to start new apps in Swift. You learn Objective-C now for a different reason: millions of lines of it still run inside iOS and macOS apps, and sooner or later a mobile developer opens a .m file and has to fix something in it.

Two facts make the language easier to read than it first looks. First, Objective-C is a strict superset of C: every valid C program is a valid Objective-C program, so printf, int and return 0; work as they do in C. Second, everything new is marked with @ or square brackets. The square brackets are message sends, an idea borrowed from Smalltalk: [greeter greet:@"Ada"] means "send the message greet: with the argument @"Ada" to the object greeter". Which code runs is decided while the program runs, not when it is compiled.

On a Mac the compiler is Apple's clang and the library is Apple's Foundation. On Linux the compiler is GCC with its Objective-C front end, and GNUstep Base provides a free implementation of the same Foundation classes (NSString, NSArray, NSLog). The main path below uses Ubuntu 26.04; each step shows the Mac command next to it. You work in ~/hello-objc-demo and delete it at the end.

Step 1 · Install the compiler and Foundation

On Ubuntu 26.04, install GCC's Objective-C compiler and the GNUstep Base development files:

Bash
sudo apt update
sudo apt install -y gobjc libgnustep-base-dev

gobjc adds Objective-C support to GCC 15.2. libgnustep-base-dev brings the Foundation headers, the library, and the helper program gnustep-config, which prints the compiler flags GNUstep needs. Check both:

Bash
gcc --version
gnustep-config --objc-flags
gnustep-config --base-libs

On a Mac, install the Xcode Command Line Tools instead. They include clang and the Foundation framework, and you do not need the full Xcode app:

Bash
xcode-select --install
clang --version

Check it worked

On Ubuntu, gcc --version starts with gcc (Ubuntu 15.2.0-...) 15.2.0. gnustep-config --objc-flags prints a long line that includes -fconstant-string-class=NSConstantString and an -I path ending in GNUstep, and --base-libs prints a line ending in -lgnustep-base -lobjc -lm. On a Mac, clang --version names Apple clang.

Step 2 · Write hello.m

Objective-C source files end in .m. Make the demo folder and create hello.m:

Bash
mkdir -p ~/hello-objc-demo
cd ~/hello-objc-demo
cat > hello.m <<'OBJC'
#import <Foundation/Foundation.h>

int main(void)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSLog(@"Hello, world!");

    [pool drain];
    return 0;
}
OBJC

Line by line:

  • #import <Foundation/Foundation.h> pulls in the declarations for Foundation's classes and functions. #import is Objective-C's version of C's #include, with one difference: it never includes the same file twice, so headers do not need include guards.
  • int main(void) is plain C. Objective-C programs start in the same main function as C programs.
  • NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; creates an autorelease pool. Foundation methods often return objects marked "free this later"; the pool is where they wait. The nested brackets are two message sends: alloc to the class NSAutoreleasePool reserves memory for a new object, and init to that new object sets it up. The * says pool is a pointer to an object, as in C. The NS prefix comes from NeXTSTEP, the system Objective-C grew up on.
  • NSLog(@"Hello, world!"); prints a log line. @"..." is an NSString object, not a C string; a plain "..." without the @ is a C char array, and NSLog will not accept it.
  • [pool drain]; frees the pool and every object waiting in it.
  • return 0; tells the operating system the program succeeded.

On a Mac you will usually see the same program written with @autoreleasepool, a block that creates and drains the pool for you:

Text
#import <Foundation/Foundation.h>

int main(void)
{
    @autoreleasepool {
        NSLog(@"Hello, world!");
    }
    return 0;
}

clang understands both forms. GCC only understands the first, so this tutorial uses NSAutoreleasePool and the same files build on both systems.

Check it worked

cat hello.m shows the eleven lines above, starting with #import.

Step 3 · Compile and run it

On Ubuntu, ask gnustep-config for the flags and pass them to gcc:

Bash
cd ~/hello-objc-demo
gcc $(gnustep-config --objc-flags) -o hello hello.m $(gnustep-config --base-libs)
./hello
  • $(gnustep-config --objc-flags) runs the helper and pastes its output into the command: where the headers live, which class to use for @"..." strings, and warnings. Put it before the source file.
  • -o hello names the program.
  • $(gnustep-config --base-libs) adds the libraries to link against: gnustep-base (Foundation) and objc (the runtime that delivers messages to objects). Libraries go after the source files.

The flags include -MMD, which makes GCC also write a small hello.d file listing the headers hello.m used. Build tools read it; you can ignore it.

On a Mac, the command is shorter because Foundation is a system framework:

Bash
clang -framework Foundation -o hello hello.m
./hello

Check it worked

The program prints one line such as 2026-11-19 10:15:02.123 hello[4397:4397] Hello, world!. NSLog adds the date and time, the program name, and the process and thread numbers. It is meant for logs, so it writes to standard error, not standard output: run ./hello 2>/dev/null and nothing appears.

Step 4 · Print plain output and read a name

For output a user should see, use C's printf. To print an NSString with it, ask the string for a C string with UTF8String. Create greet-plain.m, which also takes a name from the command line:

Bash
cd ~/hello-objc-demo
cat > greet-plain.m <<'OBJC'
#import <Foundation/Foundation.h>
#include <stdio.h>

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

    NSString *name = @"world";
    if (argc > 1) {
        name = [NSString stringWithUTF8String:argv[1]];
    }

    NSString *message = [NSString stringWithFormat:@"Hello, %@! Your name has %lu characters.",
                                                   name, (unsigned long)[name length]];
    printf("%s\n", [message UTF8String]);

    [pool drain];
    return 0;
}
OBJC
gcc $(gnustep-config --objc-flags) -o greet-plain greet-plain.m $(gnustep-config --base-libs)
./greet-plain
./greet-plain Ada
./greet-plain Adébáyọ̀

What is new:

  • #include <stdio.h> declares printf, which comes from C's standard input/output header, not from Foundation.
  • int main(int argc, char *argv[]) is the C way to receive command-line words. argc counts them and argv[1] is the first one you type.
  • [NSString stringWithUTF8String:argv[1]] turns the C string into an NSString. The colon is part of the method's name: stringWithUTF8String: takes one argument.
  • stringWithFormat: builds a string like printf does. %@ inserts any object, here the name. %lu with the (unsigned long) cast prints the length, a C number.
  • [name length] counts UTF-16 code units, not bytes. For "Ada" that is 3.
  • printf("%s\n", [message UTF8String]); prints the result as plain text with no timestamp.

The Mac build is clang -framework Foundation -o greet-plain greet-plain.m.

Check it worked

The three runs print Hello, world! Your name has 5 characters., Hello, Ada! Your name has 3 characters. and a line greeting Adébáyọ̀ with no date in front. The Yoruba name has 7 letters but reports 8 characters: é, á and ọ are each one code unit, but ọ̀ has no single precomposed form, so its grave accent is stored as a separate combining mark. A different keyboard or input method may send the accents as separate marks (decomposed text) and give a higher count for the same name. Keep that in mind before you use length to limit names typed in Yoruba, Igbo, Amharic or any language with combining marks.

Step 5 · Write a Greeter class with a header and an implementation

Objective-C splits a class into two files. The header (.h) holds the @interface: the class's name, its parent class and the messages it answers. The implementation (.m) holds the @implementation: the code. Other files import only the header. Create the header:

Bash
cd ~/hello-objc-demo
cat > Greeter.h <<'OBJC'
#import <Foundation/Foundation.h>

@interface Greeter : NSObject

- (NSString *)greet:(NSString *)name;

@end
OBJC
  • @interface Greeter : NSObject declares a class called Greeter whose parent is NSObject, the root class that provides alloc, init and memory management.
  • - (NSString *)greet:(NSString *)name; declares an instance method. The - means it is sent to an object (a + would mean the class itself). It returns an NSString *, its name is greet:, and its one argument is called name.
  • @end closes the interface.

Now the implementation:

Bash
cd ~/hello-objc-demo
cat > Greeter.m <<'OBJC'
#import "Greeter.h"

@implementation Greeter

- (NSString *)greet:(NSString *)name
{
    return [NSString stringWithFormat:@"Hello, %@, from a Greeter.", name];
}

@end
OBJC

#import "Greeter.h" uses quotes, not angle brackets, because the header is in your own folder. The method body repeats the declaration and then does the work.

Check it worked

ls ~/hello-objc-demo lists Greeter.h and Greeter.m. grep -c '@end' Greeter.h Greeter.m prints 1 for each file.

Step 6 · Send your first message

Write a main.m that creates a Greeter and sends it greet::

Bash
cd ~/hello-objc-demo
cat > main.m <<'OBJC'
#import <Foundation/Foundation.h>
#import "Greeter.h"
#include <stdio.h>

int main(void)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    Greeter *greeter = [[Greeter alloc] init];
    NSString *line = [greeter greet:@"Ada"];
    printf("%s\n", [line UTF8String]);

    [greeter release];
    [pool drain];
    return 0;
}
OBJC
gcc $(gnustep-config --objc-flags) -o greeter main.m Greeter.m $(gnustep-config --base-libs)
./greeter
  • #include <stdio.h> again declares printf, from C's standard input/output header.
  • Greeter *greeter = [[Greeter alloc] init]; makes a new Greeter, exactly as you made the pool.
  • [greeter greet:@"Ada"] is the message send. At run time, the Objective-C runtime looks up the method named greet: on the object's class and calls it.
  • [greeter release]; says you are finished with the object. You created it with alloc, so you own it and must release it. The string from stringWithFormat: was not made with alloc, so the pool frees it instead.
  • The gcc line now lists both .m files. Each is compiled separately, and the linker joins them.

On a Mac, build it with clang -framework Foundation -o greeter main.m Greeter.m.

Xcode projects switch on Automatic Reference Counting (ARC, the clang flag -fobjc-arc). With ARC the compiler writes the release calls for you, so you must delete [greeter release], and NSAutoreleasePool is replaced by @autoreleasepool. Most modern Objective-C code you read is written for ARC. GCC does not support ARC, which is why this tutorial manages memory by hand.

Check it worked

./greeter prints Hello, Ada, from a Greeter. Change @"Ada" to your own name in main.m, build and run again, and the greeting changes.

Step 7 · Recognise the errors beginners hit

Make each mistake on purpose so you recognise it later.

Compiling without the GNUstep flags. Leave out the gnustep-config parts:

Bash
cd ~/hello-objc-demo
gcc -o hello hello.m

GCC stops with fatal error: Foundation/Foundation.h: No such file or directory. The header exists, but GCC only finds it through the -I path that gnustep-config --objc-flags supplies. On a Mac the matching mistake is leaving out -framework Foundation, which ends in a linker error about undefined symbols.

Using @autoreleasepool with GCC. Copy the Mac version of Hello World and build it on Ubuntu:

Bash
cd ~/hello-objc-demo
cat > pool-block.m <<'OBJC'
#import <Foundation/Foundation.h>

int main(void)
{
    @autoreleasepool {
        NSLog(@"Hello, world!");
    }
    return 0;
}
OBJC
gcc $(gnustep-config --objc-flags) -o pool-block pool-block.m $(gnustep-config --base-libs) || echo "build failed"

GCC reports error: stray '@' in program, usually followed by a few more errors about the lines after it; those follow-on errors all come from the same @. The code is fine for clang; GCC's Objective-C has not added this keyword. Use the NSAutoreleasePool form on Linux.

Sending a message the object does not understand. In main.m, misspelling greet: as greets: still compiles, with only a warning, because messages are looked up at run time:

Bash
cd ~/hello-objc-demo
sed 's/greet:@/greets:@/' main.m > typo.m
gcc $(gnustep-config --objc-flags) -o typo typo.m Greeter.m $(gnustep-config --base-libs)
./typo

The build warns 'Greeter' may not respond to '-greets:'. Running it stops with Uncaught exception NSInvalidArgumentException, reason: -[Greeter greets:]: unrecognized selector sent to instance .... Treat that warning as an error: it is the compiler telling you the message has no matching method.

Check it worked

You have seen Foundation/Foundation.h: No such file or directory, stray '@' in program (followed by build failed) and unrecognized selector in your own terminal, and you know which fix goes with each.

Step 8 · Clean up

Delete the demo folder:

Bash
cd ~
rm -rf ~/hello-objc-demo

If you will not write more Objective-C on Linux, remove the packages with sudo apt remove gobjc libgnustep-base-dev. On a Mac, keep the Command Line Tools; Git and many other tools use them.

Check it worked

ls ~/hello-objc-demo reports No such file or directory.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.