Skip to content

Instantly share code, notes, and snippets.

@jetsonhacks
Created February 25, 2025 05:51
Show Gist options
  • Save jetsonhacks/87a12c2dca43c942feb1a41d3cd98c52 to your computer and use it in GitHub Desktop.
Save jetsonhacks/87a12c2dca43c942feb1a41d3cd98c52 to your computer and use it in GitHub Desktop.
Utility script: check_so_dependencies.sh
#!/bin/bash
# Check if a library name or path was provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <library.so>"
echo "Example: $0 libopencv_core.so"
echo " $0 /path/to/cv2.cpython-38-aarch64-linux-gnu.so"
exit 1
fi
LIBRARY="$1"
# Function to find the library if only a name (not a full path) is given
find_library() {
local libname="$1"
# Strip 'lib' prefix and '.so' suffix if present, then search with ldconfig
local basename=$(echo "$libname" | sed 's/^lib\(.*\)\.so$/\1/' | sed 's/\.so$//')
local found=$(ldconfig -p | grep "lib${basename}.so" | head -n 1 | awk '{print $NF}')
echo "$found"
}
# Determine if the input is a full path or just a library name
if [[ "$LIBRARY" == /* ]]; then
# Full path provided
if [ ! -f "$LIBRARY" ]; then
echo "Error: Library file '$LIBRARY' not found."
exit 1
fi
LIB_PATH="$LIBRARY"
else
# Library name provided (e.g., libopencv_core.so)
LIB_PATH=$(find_library "$LIBRARY")
if [ -z "$LIB_PATH" ]; then
echo "Error: Library '$LIBRARY' not found on the system."
echo "Searched using ldconfig -p; ensure it's installed or provide the full path."
exit 1
fi
echo "Found library at: $LIB_PATH"
fi
# Check if the file is a valid shared library
if ! file "$LIB_PATH" | grep -q "ELF.*shared object"; then
echo "Error: '$LIB_PATH' is not a valid shared library."
exit 1
fi
# List dependencies and check for missing ones
echo "Checking dependencies for: $LIB_PATH"
echo "----------------------------------------"
ldd "$LIB_PATH" | while read -r line; do
# Extract library name and status
if echo "$line" | grep -q "=>"; then
LIB_NAME=$(echo "$line" | awk '{print $1}')
LIB_LOCATION=$(echo "$line" | awk '{print $3}')
if echo "$line" | grep -q "not found"; then
echo "Missing: $LIB_NAME"
else
echo "Found: $LIB_NAME => $LIB_LOCATION"
fi
fi
done
echo "----------------------------------------"
echo "Summary of missing dependencies:"
ldd "$LIB_PATH" | grep "not found" | awk '{print $1}' | while read -r missing; do
echo "- $missing"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment