Skip to content
Latchkey

ld "undefined reference to" - Fix Linker Errors in CI

Compilation succeeded, but the linker could not find the definition of a symbol your code calls. A library is missing from the link line, the link order is wrong, or the function is declared but never defined.

What this error means

The build compiles every object, then fails at link time with "undefined reference to <symbol>", followed by "collect2: error: ld returned 1 exit status". The symbol exists somewhere - it just was not on the link line.

ld output
/usr/bin/ld: main.o: in function `main':
main.cpp:(.text+0x2a): undefined reference to `SSL_library_init'
collect2: error: ld returned 1 exit status

Common causes

A library is not linked

The header was included (so it compiled) but the library implementing the symbol was never passed to the linker (e.g. missing -lssl -lcrypto, or target_link_libraries omits it).

Wrong link order

With static libraries the linker resolves left to right; a library must appear after the objects that use it. Listing it too early leaves the symbol unresolved.

How to fix it

Link the library that defines the symbol

Add the library to the link line, after the objects that use it.

Terminal
g++ main.o -o app -lssl -lcrypto
# CMake:
# target_link_libraries(app PRIVATE OpenSSL::SSL OpenSSL::Crypto)

Fix link order or declare the definition

  1. Place libraries after the objects that reference them on the link line.
  2. If the symbol is your own, confirm the .cpp defining it is compiled and linked.
  3. For circular static-library deps, repeat the library or use -Wl,--start-group ... --end-group.

How to prevent it

  • Use find_package + target_link_libraries so libraries and order are managed by CMake.
  • Keep declarations and definitions in sync; build all source files.
  • Prefer imported targets over raw -l flags to avoid order pitfalls.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →