C Compilation and Linking

Advanced
6 min read
Updated: Nov 2026
1 example

What Is Compilation and Linking?

Building a C program has four stages. The preprocessor expands macros and includes (gcc -E). The compiler translates C to assembly (-S). The assembler creates object files (-c). The linker combines object files and libraries into an executable and resolves each symbol. Errors differ by stage: syntax errors at compile time, undefined reference at link time.

Remember
gcc -E main.c           # preprocess only
gcc -S main.c           # to assembly
gcc -c main.c           # to object file main.o
gcc main.o util.o -o app   # link
gcc -Wall -Wextra -std=c11 main.c util.c -o app

Cheat Sheet

Keep these facts handy while you work with Compilation and Linking in C.

TopicDetail
-EPreprocessed source
-cObject file, no linking
-lLink a library: -lm for math
Linker errorundefined reference to ...

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: const Qualifier.

Example 1 — Two source files, one program

Compile both and link: gcc -Wall main.c util.c -o app.

C
/* util.c */
int add(int a, int b) { return a + b; }

/* main.c */
#include <stdio.h>
int add(int a, int b);   /* declaration; defined in util.c */

int main(void) {
    printf("add(3, 4) = %d\n", add(3, 4));
    return 0;
}

How It Works

  1. main.c compiles because the declaration tells the compiler the signature.
  2. The call is left unresolved in main.o.
  3. The linker finds add in util.o and completes the program.

Edge Cases & Pitfalls

Check these before relying on Compilation and Linking in real programs.

order

Library order

With GNU ld, put libraries after the files that use them: gcc main.c -lm.

undef

Undefined reference

It means you declared a function but never defined it, or forgot to compile or link the file that does.

Key Takeaways

  • Stages: preprocess, compile, assemble, link.
  • Compile errors vs link errors.
  • Separate compilation speeds rebuilds.

One line: Building a C program has four stages.

Frequently Asked Questions

Compiling turns each source file into an object file. Linking combines object files and libraries and resolves cross-file references into one executable.
The linker could not find the definition of a function or variable that was declared and used. Check that every needed file or library is on the command line.
Continue with "C const Qualifier" next. Mark data as read-only to catch bugs and document intent. The C Advanced Concepts hub lists every topic in order.

Next: const Qualifier

Mark data as read-only to catch bugs and document intent.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful