Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

Compiling with clang and GNUstep

Learn the Objective-C build pipeline, the essential clang flags, how to compile multi-file programs with a Makefile, and how to read compiler errors and warnings.

Writing code is only half the job. To get a running program you must compile each .m file to an object file and then link the objects with the libraries they use. IDEs hide these steps, but understanding them makes errors much less mysterious. This lesson covers the pipeline, the flags you will use every day, multi-file builds with a Makefile, and how to turn warnings into a helpful safety net.

The build pipeline

flowchart LR
  A["main.m"] --> P["Preprocessor: #import, #define"]
  B["Greeter.m"] --> P
  P --> C["Compiler (clang): parse, check, generate code"]
  C --> O["main.o and Greeter.o"]
  O --> L["Linker: adds Foundation and the runtime"]
  L --> X["Executable"]

Each source file is compiled separately into an object file. The linker joins them and resolves references to libraries.

  1. The preprocessor expands #import, #define and conditional blocks.
  2. The compiler turns each translation unit into machine code, checking types and message names against the declared interfaces.
  3. The linker combines the objects with Foundation and the Objective-C runtime to produce one executable.

A header only declares things; the implementation exists in a .m file and is linked in. That is why a missing .m file produces a linker error even when the compile succeeded.

Single-file builds

On macOS:

Terminal
clang -fobjc-arc -framework Foundation main.m -o app

On Linux with GNUstep:

Terminal
clang -fobjc-arc -fobjc-runtime=gnustep-2.0 \
  $(gnustep-config --objc-flags) main.m \
  $(gnustep-config --base-libs) -o app

The flags in order:

Flag Meaning
-fobjc-arc Turn on Automatic Reference Counting
-framework Foundation Link Apple's Foundation framework (macOS only)
-o app Name the output file
-Wall -Wextra Enable many useful warnings
-Werror Treat warnings as errors
-g Include debug symbols for the debugger
-O0 / -O2 No optimization / normal optimization
-c Compile only, produce a .o file and do not link
-std=gnu11 Choose the C language standard
-DDEBUG=1 Define a preprocessor macro

Multiple files

Suppose you have main.m, Greeter.m and Greeter.h. You can pass them all to clang at once:

Terminal
clang -fobjc-arc -framework Foundation main.m Greeter.m -o app

For larger programs, compile each file separately so that changing one file does not rebuild the others:

Terminal
clang -fobjc-arc -c Greeter.m -o Greeter.o
clang -fobjc-arc -c main.m    -o main.o
clang -framework Foundation Greeter.o main.o -o app

A Makefile

make automates this and rebuilds only what changed. The file below works on macOS; on Linux replace the LDFLAGS line with the output of gnustep-config --base-libs.

MAKEFILE
CC       = clang
CFLAGS   = -fobjc-arc -Wall -Wextra -g
LDFLAGS  = -framework Foundation
OBJECTS  = main.o Greeter.o

app: $(OBJECTS)
	$(CC) $(OBJECTS) $(LDFLAGS) -o app

%.o: %.m Greeter.h
	$(CC) $(CFLAGS) -c $< -o $@

clean:
	rm -f $(OBJECTS) app

Lines under a rule must start with a tab, not spaces. Type make to build and make clean to remove build products.

Reading errors and warnings

A compiler error stops the build. A warning does not, but it usually points at a real bug. Compile with -Wall -Wextra and read them.

TEXT
main.m:7:9: warning: instance method '-sayHelo' not found
      (return type defaults to 'id') [-Wobjc-method-access]
        [greeter sayHelo];
        ^

This tells you the file, line and column, and that you misspelled sayHello. Typos in a message name are only warnings in Objective-C (they become a crash at run time with "unrecognized selector"), so treating warnings as errors with -Werror is a good habit.

Typical build problems:

  • use of undeclared identifier: a missing #import or a typo.
  • Undefined symbols for architecture (linker): a .m file was not part of the build, or a library is missing.
  • ARC forbids ...: you used retain, release or autorelease while ARC is on.
  • Unknown receiver 'Greeter': the class header was not imported.

Stand-in: conditional compilation

Build flags like -DDEBUG change what code is compiled. This C program shows the mechanism. Add or remove the #define lines to change the result. The __OBJC__ macro is defined automatically when a file is compiled as Objective-C, so here we show the plain C branch.

C Stand-in (C): build flags as macros

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.

Stand-in: declaration versus definition

The compiler needs a declaration to type-check a call; the linker needs one definition. Here both live in one file, but the layout mimics a header (declarations first) followed by an implementation.

C Stand-in (C): header-style declarations, then definitions

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.

If you delete the last block, the file still compiles as far as main goes, but linking fails. That is exactly the "missing .m file" situation.

Using Xcode

In Xcode all this is configured for you: add source files to a target, and Xcode compiles each .m file and links the frameworks listed under "Frameworks and Libraries". Build settings such as "Objective-C Automatic Reference Counting" correspond to the clang flags above. The Issue navigator lists warnings and errors, and the Product > Analyze command runs the static analyzer that finds memory and logic bugs without running the code.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which stage joins several object files and libraries into one program?

    The linker combines objects with libraries such as Foundation and resolves symbols.
  2. 2. What does the -c flag do?

    -c stops after compilation and writes a .o file, which is how Makefiles build one file at a time.
  3. 3. You forgot to compile Greeter.m. What kind of error do you most likely see?

    The header declared the class, so compilation succeeds, but the linker cannot find its code.
  4. 4. Why use -Wall -Wextra -Werror in Objective-C?

    Unknown message names are reported as warnings, so enabling warnings and treating them as errors catches the typo before it crashes.

Summary

  • Building has three stages: preprocess, compile each file, then link.
  • Key flags: -fobjc-arc, -framework Foundation (or GNUstep libs), -Wall -Wextra, -g, -c, -o.
  • Compile each .m file separately and use a Makefile to rebuild only what changed.
  • Read warnings carefully: a misspelled message is often just a warning.
  • Conditional compilation with -D macros switches debug code on and off.

Next: Variables and Data Types