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-sectionsCommon 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
-lafter the objects. - "cannot find -lfoo" means the library is not on any
-Lsearch path.
Related guides
ldd Command: Check Shared Libraries in CIldd prints the shared libraries a binary needs. Reference for -v, -u, -d, -r, and how to debug "not found" li…
gcc Command: Compile C in CIgcc is the GNU C compiler driver. Reference for -c, -o, -I, -L, -l, -Wall, -O2, -std, and the header/library…
clang Command: Compile C/C++ in CIclang is the LLVM C/C++ compiler. Reference for -c, -o, -I, -std, -fsanitize, -Weverything, and the diagnosti…