针对linux编译好的程序,一键打包所以来的动态库,脚本如下;
#!/bin/bash
# 检查参数数量
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <source_executable> <destination_directory>"
exit 1
fi
# 读取参数
SOURCE_EXECUTABLE=$1
DESTINATION_DIR=$2
# 确保目标目录存在,如果不存在则创建
mkdir -p "$DESTINATION_DIR"
# 复制可执行文件
cp "$SOURCE_EXECUTABLE" "$DESTINATION_DIR"
# 获取可执行文件的完整路径
EXECUTABLE_NAME=$(basename "$SOURCE_EXECUTABLE")
DEST_EXECUTABLE="$DESTINATION_DIR/$EXECUTABLE_NAME"
# 使用ldd获取动态库依赖,并复制它们
LD_LIBRARY_PATH=$(dirname "$SOURCE_EXECUTABLE"):$LD_LIBRARY_PATH
DEPENDENCIES=$(ldd "$SOURCE_EXECUTABLE" | grep '=>' | awk '{print $3}')
for LIB in $DEPENDENCIES; do
# 复制动态库到目标目录
cp "$LIB" "$DESTINATION_DIR"
done
echo "Executable and its dependencies have been copied to $DESTINATION_DIR"
使用方法:
./pack.sh [可执行文件] [目标目录]