gcc hello.c -o hello gcc behind the scene
from chatGPT
i just changed hello.c to hello.sh.
then compile use gcc, it reports error. why?
When you issue the command:
gcc hello.c -o hello
GCC performs four major stages by default, unless told otherwise. These are:
1. Preprocessing
-
Tool involved:
cpp -
Input:
hello.c -
Output: preprocessed source (
.ifile, internally) -
Purpose:
-
Expands macros (
#define) -
Includes headers (
#include) -
Removes comments
-
Processes conditional compilation (
#ifdef, etc.)
-
✅ Result: a "flattened" version of the C code, ready for parsing.
2. Compilation
-
Tool involved:
cc1 -
Input: preprocessed
.ifile -
Output: assembly code (
.sfile, internally) -
Purpose:
-
Parses and analyzes the C code
-
Performs syntax and semantic checks
-
Converts C code into architecture-specific assembly language
-
✅ Result: human-readable assembly instructions (but not yet executable).
3. Assembly
-
Tool involved:
as(the assembler) -
Input: assembly file (
.s) -
Output: object file (
.o) -
Purpose:
-
Converts assembly code into machine code (binary)
-
Produces a relocatable object file (not yet a full program)
-
✅ Result: raw object code — the actual CPU instructions.
4. Linking
-
Tool involved:
ld(the linker) -
Input: object file(s) + standard libraries (e.g.,
libc) -
Output: executable (
hello) -
Purpose:
-
Combines your object file with startup code (
crt0), standard libraries (likeprintf), etc. -
Resolves symbols (e.g., links
mainto_start) -
Produces a complete, executable binary
-
✅ Result: A self-contained executable program.
Summary Table:
| Stage | Tool | Input | Output | Description |
|---|---|---|---|---|
| Preprocessing | cpp |
hello.c |
hello.i |
Handles macros, headers, etc. |
| Compilation | cc1 |
hello.i |
hello.s |
C → Assembly |
| Assembly | as |
hello.s |
hello.o |
Assembly → Object Code |
| Linking | ld |
hello.o, libs |
hello |
Object code → Executable |
Optional:
You can inspect each stage manually:
gcc -E hello.c -o hello.i # Only preprocessing
gcc -S hello.c -o hello.s # Compile to assembly
gcc -c hello.c -o hello.o # Compile+assemble to object
gcc hello.o -o hello # Link only
This gives you fine-grained control over the pipeline.
Would you like a diagram of this pipeline, or want to step through it with a concrete example?

浙公网安备 33010602011771号