Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Understanding LD_LIBRARY_PATH in Linux

Notes 1

Setting Dynamic Library Paths via Command Line

To configure the dynamic linker search path at runtime, use:

export LD_LIBRARY_PATH=$(pwd)/library_dir:$LD_LIBRARY_PATH

This command prepends the current directory's library_dir subdirectory to the existing library search path. Key components:

  • export: Makes the variible available to child processes
  • LD_LIBRARY_PATH: Colon-separated list of directories for shared libraries (.so files)
  • $PWD/library_dir: Path to additional library directory
  • :$LD_LIBRARY_PATH: Appends existing paths

Useful for temporary/test environments but avoid permanent usage due to potential version conflicts.

LD_LIBRARY_PATH Deep Dive

A colon-delimited environment variable specifying additional directories for dynamic linker searches beyond standard locations (/lib, /usr/lib).

Configuration Syntax

export LD_LIBRARY_PATH=/custom/lib/path:$DYNAMIC_LIB_PATH

Replace /custom/lib/path with actual directory. Multiple paths require colon separation.

Practical Example

For libraries in /home/user/project_libs:

export LD_LIBRARY_PATH=/home/user/project_libs:$DYNAMIC_LIB_PATH

The dynamic linker checks this directory first before standard paths.

Caveats

  1. Security: Risk of loading malicious/unintended libraries
  2. Portability: Environment-dependent execution failures
  3. Maintenance: Complex dependency management in multi-user systems

Prefer standard installation paths or compiler-level configurations where possible.

Alternative Approaches

Stendard System Paths

Install libraries to default locations (/usr/lib, /lib) requiring administrative privileges.

Compiler-Level Path Specification

Embed paths directly in binaries using linker options:

RPATH (Hard-coded paths):

gcc -o application main.c -L/custom/lib/dir -lcoollib -Wl,-rpath,/custom/lib/dir
  • -L: Compilation-time library search path
  • -l: Library name
  • -Wl,-rpath: Embeds runtime search path

RUNPATH (Environment-overridable):

gcc -o application main.c -L/custom/lib/dir -lcoollib -Wl,--enable-new-dtags,-rpath,/custom/lib/dir

Prioritizes LD_LIBRARY_PATH over embedded paths.

Selection Guidance

  • Use RPATH for immutable deployment paths
  • Choose RUNPATH when runtime flexibility is required

CMake Implementation

RPATH Configuration

Modern approach avoiding environment variables:

cmake_minimum_required(VERSION 3.10)
project(SampleApp)

set(CMAKE_CXX_STANDARD 17)
add_executable(sample_exe source.cpp)
target_link_libraries(sample_exe coollib)

# Build-tree RPATH
set(CMAKE_BUILD_RPATH "/custom/build/lib")
# Install-tree RPATH
set(CMAKE_INSTALL_RPATH "/custom/install/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

Target-Specific Properties

set_target_properties(application PROPERTIES
    BUILD_RPATH "${CMAKE_SOURCE_DIR}/build_libs"
    INSTALL_RPATH "${CMAKE_SOURCE_DIR}/install_libs"
    INSTALL_RPATH_USE_LINK_PATH TRUE
)

Configures runtime search paths for specific targets during build/installation.

Custom Build Commands (Legacy Approach)

add_custom_command(TARGET sample_exe POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH="/custom/runtime/libs" $<TARGET_FILE:sample_exe>
)

Sets temporary environment during post-build execution.

Recommendations

  1. Prefer system package managers for library management
  2. Use RPATH/RUNPATH for self-contained deployments
  3. Reserve LD_LIBRARY_PATH for development/testing scenarios
  4. Document all non-standard library dependencies
Tags: Linux

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.