Skip to content
Latchkey

Linker "undefined reference to" a symbol in CI

The compiler accepted the code, but the linker could not find the definition of a symbol you use. In package builds this usually means a library was not linked, was linked in the wrong order, or a dependency the package provides is missing from the link line.

What this error means

The build reaches the link stage and fails with "undefined reference to 'X'", often for a function from a library you expected the build system to link.

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

Common causes

A library is not linked

The symbol lives in a library (for example libssl) that is not passed to the linker, so its definition is never found.

Wrong link order between objects and libraries

With static libraries the linker resolves left to right, so a library listed before the object that needs it leaves the symbol unresolved.

How to fix it

Link the providing library after your objects

Add the library and place it after the objects that reference it.

Terminal
g++ main.o -o app -lssl -lcrypto

Declare the dependency in the build system

In CMake, link the imported target so the flags and order are handled for you.

CMakeLists.txt
find_package(OpenSSL REQUIRED)
target_link_libraries(app PRIVATE OpenSSL::SSL OpenSSL::Crypto)

How to prevent it

  • Link libraries through CMake imported targets so order is correct.
  • Install the library dev package so its archive or shared object exists.
  • Keep the link line generated by the build system rather than hand-written.

Related guides

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