The Compiler Driver
gcc is not really a compiler. It is a driver: a small program that looks at your file names and flags, decides which stages are needed, and runs the real tools one after another. Knowing this explains a lot of build behaviour: why gcc can link C++ objects (badly), why -c stops early, and why "the compiler" can fail with a message from ld.
Which tools run?
Two flags make the driver show its plan. -v prints each command as it runs; -### prints the commands without running them. The commands are very long, so this session keeps only the first word of each:
mkdir -p drv && cd drv
printf '#include <stdio.h>\nint main(void) { puts("hi"); return 0; }\n' > hello.c
gcc --version | head -1
gcc -### hello.c -o hello 2>&1 | grep '^ ' | awk '{print $1}'
echo "--- clang"
clang --version | head -1
clang -### hello.c -o hello 2>&1 | grep '^ ' | awk '{print $1}'gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
/usr/libexec/gcc/x86_64-linux-gnu/13/cc1
as
/usr/libexec/gcc/x86_64-linux-gnu/13/collect2
--- clang
Ubuntu clang version 18.1.3 (1ubuntu1)
"/usr/lib/llvm-18/bin/clang"
"/usr/bin/ld"
- GCC runs three programs:
cc1(preprocessor and compiler in one process, writes assembly),as(the GNU assembler from binutils, writes an object file) andcollect2, a GCC wrapper around the linkerld. - Clang runs itself again in
-cc1mode, which preprocesses, compiles and assembles in one process (Clang has an integrated assembler), then runs the system linker/usr/bin/ld.
You can confirm the real process list with strace, which logs every execve (program start). The -o writes the trace to a file so it doesn't mix with the program's output:
mkdir -p drv && cd drv
printf 'int main(void) { return 0; }\n' > t.c
strace -f -qq -e trace=execve -e signal=none -o trace.txt gcc t.c -o t
grep -v ENOENT trace.txt | grep -o 'execve("[^"]*"' | sed 's/execve(//'"/usr/bin/gcc"
"/usr/libexec/gcc/x86_64-linux-gnu/13/cc1"
"/usr/bin/as"
"/usr/libexec/gcc/x86_64-linux-gnu/13/collect2"
"/usr/bin/ld"
grep -v ENOENT hides the failed attempts: the driver searches $PATH for as, and every directory where it isn't found shows up as an error in the raw trace. Toolkit: binutils, strace & ltrace has more on strace.
Where the pieces live
The driver also knows where its own parts and the C library are. These queries are useful when a build picks up the wrong tool or library:
gcc -dumpmachine
gcc -print-prog-name=cc1
gcc -print-file-name=crt1.o
gcc -print-file-name=libc.so
clang -dumpmachinex86_64-linux-gnu
/usr/libexec/gcc/x86_64-linux-gnu/13/cc1
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libc.so
x86_64-pc-linux-gnu
-dumpmachine prints the target triple (CPU, vendor, OS/ABI) the compiler produces code for. GCC says x86_64-linux-gnu, Clang spells the same platform x86_64-pc-linux-gnu. Triples matter as soon as you build for another machine; see Cross-Compilation. crt1.o is C start-up code that the driver quietly adds to every executable: it sets up the process and calls main.
Stopping early
The driver picks its starting stage from the input file's extension and its last stage from a flag:
| Input | Starts at | Flag | Stops after | Output |
|---|---|---|---|---|
.c |
preprocessing | -E |
preprocessing | stdout (or -o file.i) |
.i |
compilation | -S |
compilation | file.s |
.s |
assembling (.S is preprocessed first) |
-c |
assembling | file.o |
.o, .a, .so |
linking | (none) | linking | a.out (or -o) |
This model of the decision is what lets you resume a build from any stage:
const STAGES = ["preprocess", "compile", "assemble", "link"];
const START = { ".c": 0, ".i": 1, ".s": 2, ".S": 0, ".o": 3, ".a": 3 };
const STOP = { "-E": 0, "-S": 1, "-c": 2 };
function plan(file, flag) {
const ext = file.slice(file.lastIndexOf("."));
const from = START[ext];
const to = flag ? STOP[flag] : 3;
if (from > to) return `${file} ${flag}: nothing to do (input is past that stage)`;
let steps = STAGES.slice(from, to + 1);
if (ext === ".S") steps = steps.filter((s) => s !== "compile"); // assembly: no C compiler needed
return `${file} ${flag ?? "(no flag)"}: ${steps.join(" -> ")}`;
}
console.log(plan("main.c"));
console.log(plan("main.c", "-c"));
console.log(plan("main.i", "-S"));
console.log(plan("boot.S", "-c"));
console.log(plan("main.o"));
console.log(plan("main.o", "-c"));main.c (no flag): preprocess -> compile -> assemble -> link
main.c -c: preprocess -> compile -> assemble
main.i -S: compile
boot.S -c: preprocess -> assemble
main.o (no flag): link
main.o -c: nothing to do (input is past that stage)
The last line is real behaviour too. GCC does not fail, it warns and produces nothing:
printf 'int main(void) { return 0; }\n' > main.c
gcc -c main.c
gcc -c main.o
echo "exit status: $?"gcc: warning: main.o: linker input file unused because linking not done
exit status: 0
Note: Use
gccfor C andg++(orclang++) for C++. They run the same stages, butg++treats.cfiles as C++ and links the C++ standard library. Linking C++ objects withgccfails with undefined references tostd::symbols.
Test yourself
Which program does gcc run to turn assembly into an object file?
cc1ascollect2ld
What does gcc -### do?
- Compiles with maximum optimisation
- Prints the commands the driver would run, without running them
- Hides all warnings
- Prints the preprocessed source
Which file does the driver add to every executable to call main?
libc.acrt1.o(C start-up code)main.ild.so