TRT
Sign in
easyFoundationsdebugging

How do you tell a compiler error apart from a linker error in C, and what causes each?

Distinguish syntax and type errors caught per translation unit by cc1 from unresolved or duplicate symbol errors caught across object files by ld.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstThe C Compilation PipelineTrace C source code through preprocessing, compilation, assembly, and linking to understand how text transforms into an executable ELF binary.

A strong answer

A compiler error happens during phase 2 of the build pipeline (cc1), when the compiler analyzes a single translation unit (.c file after preprocessing) in isolation. It is caused by violations of C grammar or type rules inside that file—such as a missing semicolon, an undeclared identifier, or passing incompatible argument types to a declared function. Compiler diagnostics always name a .c or .h source file, a line number, and a column number.

A linker error happens during phase 4 (ld), after every .c file has already compiled cleanly into a relocatable .o object file. When code calls a function declared via a prototype (such as double compute(double x);), the compiler trusts the promise and emits a call instruction with a placeholder relocation entry. The linker must connect that relocation to the single object file or library that defines compute. Linker errors fall into two main categories: undefined reference (no object file or linked library provides the definition, or main itself is missing) and multiple definition (two or more object files define the same external symbol, violating the One Definition Rule).

What the interviewer is listening for

  • Knows that the compiler checks one translation unit at a time using declarations/prototypes, while the linker resolves symbols across all .o files and libraries.
  • Recognizes undefined reference to 'foo' and multiple definition of 'bar' as linker (ld) diagnostics.
  • Explains how missing -l<lib> flags or uncompiled .c files cause undefined references even when the right #include header is present.

Common mistakes

  • Re-editing header #include lines to fix an undefined reference error when the header was already included and the real fix is linking the .c or -l library.
  • Putting function or global variable definitions (without static or inline) inside a .h header file included by multiple .c files, causing a multiple-definition linker error.
  • Confusing compile-time header search paths (-I) with link-time library search paths (-L and -l).

This answer walks

Practice follow-ups

  1. 01What happens if you include a header with a function prototype but forget to pass the library flag -lm to gcc?
  2. 02Why does defining a non-static global variable in a header file included by two .c files fail at link time rather than compile time?

Not started

Sign in to save your learning progress.

Sign in to save