Understanding LD_LIBRARY_PATH in Linux
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 processesLD_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
- Security: Risk of loading malicious/unintended libraries
- Portability: Environment-dependent execution failures
- 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
- Prefer system package managers for library management
- Use RPATH/RUNPATH for self-contained deployments
- Reserve
LD_LIBRARY_PATHfor development/testing scenarios - Document all non-standard library dependencies