• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

ld Command Examples

by admin

The “ld” command, short for “linker,” is a critical tool in the software development process, responsible for linking object files together to produce executable programs or shared libraries. When programming in languages like C or C++, the source code is typically compiled into object files containing machine code instructions and data. However, these object files alone cannot be executed; they need to be linked together with other necessary files to form a complete executable program.

Here’s how the “ld” linker works:

  • Object File Linking: The primary function of “ld” is to take one or more object files (typically ending with the “.o” extension) and link them together to create a single executable file or shared library. It resolves references between different object files, such as function calls or variable accesses, ensuring that the final program behaves correctly.
  • Symbol Resolution: During the linking process, “ld” resolves symbols defined in one object file with references to those symbols in other object files. This includes functions, global variables, and other symbols used across multiple source files. If a symbol cannot be resolved, “ld” generates an error indicating an unresolved symbol.
  • Library Linking: In addition to object files, “ld” can also link in external libraries containing precompiled code and functions. These libraries, often stored as archive files (with extensions like “.a” on Unix systems), contain reusable code that can be linked into multiple programs. “ld” searches these libraries for symbols referenced by the object files being linked, automatically pulling in the necessary code to satisfy dependencies.
  • Output Generation: Once all necessary object files and libraries have been linked together, “ld” generates the final executable program or shared library. The output file can be specified using command-line options, allowing users to control the name and format of the generated output.
  • Customization and Configuration: “ld” provides various options and directives to customize the linking process. Users can specify linker scripts to control the layout and organization of the output file, define custom symbol aliases or transformations, and specify linker options to optimize code generation or control symbol visibility.

ld Command Examples

1. Link a specific object file with no dependencies into an executable:

# ld [path/to/file.o] --output [path/to/output_executable]

2. Link two object files together:

# ld [path/to/file1.o] [path/to/file2.o] --output [path/to/output_executable]

3. Dynamically link an x86_64 program to glibc (file paths change depending on the system):

# ld --output [path/to/output_executable] --dynamic-linker /lib/ld-linux-x86-64.so.2 /lib/crt1.o /lib/crti.o -lc [path/to/file.o] /lib/crtn.o

Summary

Overall, the “ld” linker is an essential tool in the software development toolchain, responsible for combining object files and libraries to create executable programs or shared libraries. Its role in symbol resolution, library linking, and output generation makes it a crucial component in the process of building complex software systems.

Filed Under: Linux

Some more articles you might also be interested in …

  1. rtcwake Command Examples in Linux
  2. rc-service Command Examples in Linux
  3. 2to3 – Automated Python 2 to 3 code conversion
  4. aspell Command Examples in Linux
  5. compgen: A built-in command for auto-completion in Bash, which is called on pressing TAB key twice
  6. kosmorro Command Examples
  7. ntpq: command not found
  8. CentOS / RHEL 6 : How to Disable / Enable direct root login via telnet
  9. rmmod Command Examples in Linux
  10. How to enable text colour in vi similar to vim in CentOS/RHEL

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright