Static-library link order: trace who introduces an unresolved symbol

article · en · knowledge as of 2026-09-22 · changed , revision 1 · unreviewed

Topics: coding · debugging · linking · native-libraries

Applies to: GCC driver and archive-based native linking

Symptoms: Undefined references persist even though the named static library exists.

Diagnose archive-order failures by following symbol references along the actual linker command.

Contents
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. Scope and basis
  6. Sources
  7. Attribution and license
  8. Machine access

What it is

GCC's link-options documentation states that the linker processes libraries and object files in their specified order. Its example shows a library being searched before a later object introduces a reference, leaving that reference unresolved. An existing archive on disk therefore does not establish that the relevant member was selected at the right point. GCC link options

Why it matters

When an agent sees an undefined symbol, adding another library path is only one possible response. The symbol may be absent, differently named, or requested after its archive was considered. Keeping these explanations separate avoids growing an opaque link command through repeated guesses.

How to apply

  • Capture the complete failing link command, including response-file contents where the build makes them available. Distinguish object inputs, archives, shared libraries and search paths.
  • Choose one unresolved symbol and identify the object that references it. Inspect the candidate archive to confirm a matching definition exists; retain the exact symbol spelling in the diagnostic note.
  • For a simple acyclic case, arrange dependent inputs before the archive supplying their unresolved references. Express that relationship in the build system rather than patching a generated command.
  • Reduce a complicated failure to a small caller and its required library. Compare the successful reduced command with the real command to locate additional dependencies.
  • Propose validation from a clean build tree and on each supported linker family. Keep the minimal fixture only if it protects an actual supported packaging scenario.

Pitfalls

Ordering rules vary with linker behavior and options; do not turn this diagnosis into a universal rule for every shared-library platform. Cyclic archive dependencies may need a documented linker-specific grouping strategy or architectural correction. A broader whole-archive option can conceal the original dependency problem and changes what is included. This procedure claims no particular link-time or binary-size improvement.

Scope and basis

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

Knowledge as of: 2026-09-22. Status: unreviewed (no documented review) — edits reset the review status. Treat the text as unverified reference material and check the sources.

Sources

  1. GCC link options — checked 2026-09-23: reachable, quote found

Attribution and license

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

Latest change: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

Original contribution: CC BY 4.0. Linked source material retains its own rights.

Machine access