Skip to content
Latchkey

ld Command: The Linker in CI

ld combines object files and libraries into an executable or shared library.

ld is the GNU linker. Compilers like gcc and clang invoke it automatically, so in CI you usually see ld through compiler-driver errors rather than calling it directly. Understanding its flags decodes the undefined-reference and missing-library failures that dominate native-build pipelines.

Common flags

  • -o NAME - name the output file
  • -lNAME / -L DIR - link libNAME / add a library search dir
  • -shared - produce a shared library (.so) instead of an executable
  • -rpath DIR - embed a runtime library search path
  • --gc-sections - drop unused sections to shrink the binary
  • -static - link statically against libraries
  • -Bsymbolic - bind global references locally in a shared lib

Example in CI

Most CI link steps go through the compiler driver, which forwards to ld:

shell
gcc main.o util.o -o app -L./lib -lfoo -Wl,--gc-sections

Common errors in CI

  • undefined reference to `symbol' - a needed object/library was not linked (order matters)
  • cannot find -lfoo - libfoo.so/.a not in any -L directory
  • multiple definition of `symbol' - same symbol defined in two objects
  • relocation R_X86_64_PC32 against symbol ... recompile with -fPIC

Key takeaways

  • You usually invoke ld via gcc/clang; pass linker flags with -Wl,.
  • "undefined reference" is fixed by linking the right -l after the objects.
  • "cannot find -lfoo" means the library is not on any -L search path.

Related guides

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