LOGBOOK

HELP

← again tap: answer good → ↑ swipe up at the end: next

1 / 10
Other keys: show • Space: good • 1-4: rate • 0: skip • 5: flag

Question

What are the steps to turn C source code into an executable?

Answer

Four stages run in sequence: preprocess → compile → assemble → link, turning .c text into a runnable binary.

The four-stage preprocess, compile, assemble, link pipeline with its artifacts.

* C becomes a program in four steps — preprocess, compile, assemble, link — each producing a concrete artifact; only the link stage pulls in libraries. *

gcc is really a driver that quietly runs four separate tools. Understanding the chain is essential for reverse engineering, because each stage produces an artifact you can inspect.

source.c → [preprocess] → .i → [compile] → .s → [assemble] → .o → [link] → executable
Stage Input Output What it does
Preprocess .c .i Expand #include, #define, conditionals
Compile .i .s Translate C into assembly text
Assemble .s .o Encode assembly into machine code (object file)
Link .o + libs executable Resolve symbols, assign addresses, pull in libraries
gcc -E source.c -o source.i   # preprocess only
gcc -S source.c -o source.s   # stop at assembly
gcc -c source.c -o source.o   # stop at object file
gcc source.o   -o program     # link to executable

Tip: gcc -S -O1 produces readable optimized assembly — the quickest way to see how the compiler turned your C into machine instructions.

Go deeper:

Illustration
Free Software Foundation, Inc. · CC BY-SA 3.0 · Wikimedia Commons
or press any other key

Question

What does the C preprocessor do?

Answer

The preprocessor is a text-substitution pass that runs first, handling every directive starting with # before the real compiler ever sees the code.

It knows nothing about C's meaning — it just edits text. Its main jobs:

  • #include — paste a header file's contents in place
  • #define — substitute macros and named constants
  • #ifdef / #ifndef — conditionally keep or drop code
  • #pragma — compiler-specific directives
#include <stdio.h>     // pastes stdio.h here
#define MAX 100        // replaces MAX with 100 everywhere
#ifdef DEBUG
    printf("debug\n"); // only compiled when DEBUG is defined
#endif

You can inspect the result with gcc -E program.c.

Classic gotcha: because macros are blind text substitution, not function calls, they bite you on precedence:

#define SQUARE(x) x*x
SQUARE(1+2)   // expands to 1+2*1+2 = 5, NOT 9

The fix is to parenthesise everything: #define SQUARE(x) ((x)*(x)).

Go deeper:

or press any other key