我的mac是m2 pro芯片,所以我的iOS模拟器也直接arm64,跟我不同的芯片的机器不保证能成功。 因为某些原因,我不能编译为动态库,所以这里是静态库,不过动态库也应该可以,自己尝试。
废话少说,先上openssl的链接github.com/openssl/ope…,下载后cd进入直接创建2个脚本:
- build_android.sh
#!/bin/bash
set -e
set -x
# 确保在OpenSSL源代码目录中运行
if [ ! -f "Configure" ]; then
echo "错误:请在OpenSSL源代码目录中运行此脚本"
exit 1
fi
export ANDROID_NDK_HOME=${ANDROID_NDK_ROOT}
# 不要在环境变量中设置CFLAGS和CXXFLAGS
unset CFLAGS
unset CXXFLAGS
# 配置环境变量 - 只添加一次路径,避免PATH膨胀
export PATH="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/bin:$PATH"
# 设置Android API
ANDROID_API=21
# 循环编译多个目标库
for openssl_arch in android-arm android-arm64
do
# 如果存在Makefile,执行clean
if [ -f "Makefile" ]; then
make clean
fi
# 直接在Configure命令行中设置-fPIC
# 添加no-asm标志来避免汇编代码的PIC问题
./Configure ${openssl_arch} -D__ANDROID_API__=$ANDROID_API no-shared no-threads -fPIC no-asm
# 构建
make -j12
# 确定对应的Android ABI名称
android_abi=""
if [ "$openssl_arch" = "android-arm" ]; then
android_abi="armeabi-v7a"
elif [ "$openssl_arch" = "android-arm64" ]; then
android_abi="arm64-v8a"
fi
# 拷贝到输出目录
OUTPUT_LIB=./build/android/${android_abi}
mkdir -p $OUTPUT_LIB
cp libcrypto.a $OUTPUT_LIB
cp libssl.a $OUTPUT_LIB
done
OUTPUT_INCLUDE=./build/android/include
mkdir -p $OUTPUT_INCLUDE
cp -RL include/openssl $OUTPUT_INCLUDE
echo "编译完成!库文件位于 ./build/android/ 目录"
echo "头文件位于 ./build/android/include/openssl 目录"
- build_ios.sh
#!/bin/zsh
set -e
set -x
# 确保在OpenSSL源代码目录中运行
if [ ! -f "Configure" ]; then
echo "错误:请在OpenSSL源代码目录中运行此脚本"
exit 1
fi
# 配置路径
IOS_SDK_VERSION=$(xcrun -sdk iphoneos --show-sdk-version)
DEVELOPER=$(xcode-select -print-path)
MIN_IOS_VERSION="12.0" # 最低支持的iOS版本
# 设置构建目录
BUILD_DIR="${PWD}/build/ios"
mkdir -p "${BUILD_DIR}"
# 函数:构建特定架构
build_for_arch() {
local ARCH=$1
local SDK_TYPE=$2
local OPENSSL_TARGET=$3
local OUTPUT_DIR="${BUILD_DIR}/${ARCH}"
echo "== 构建 ${ARCH} 架构 =="
# 创建输出目录
mkdir -p "${OUTPUT_DIR}"
# 设置编译环境变量
if [[ "$ARCH" == *"simulator"* ]]; then
# 模拟器构建
SDK_PATH=$(xcrun --sdk iphonesimulator --show-sdk-path)
export CROSS_TOP="${DEVELOPER}/Platforms/iPhoneSimulator.platform/Developer"
export CROSS_SDK="iPhoneSimulator${IOS_SDK_VERSION}.sdk"
export CC="clang -arch arm64"
export CFLAGS="-isysroot ${SDK_PATH} -mios-simulator-version-min=${MIN_IOS_VERSION} -fPIC"
else
# 设备构建
SDK_PATH=$(xcrun --sdk iphoneos --show-sdk-path)
export CROSS_TOP="${DEVELOPER}/Platforms/iPhoneOS.platform/Developer"
export CROSS_SDK="iPhoneOS${IOS_SDK_VERSION}.sdk"
export CC="clang -arch arm64"
export CFLAGS="-isysroot ${SDK_PATH} -mios-version-min=${MIN_IOS_VERSION} -fPIC"
fi
# 为链接器设置rpath(虽然对静态库来说不是必需的)
if [[ "$ARCH" == *"simulator"* ]]; then
export LDFLAGS="-Wl,-rpath,@executable_path/Frameworks"
else
export LDFLAGS="-Wl,-rpath,@executable_path/Frameworks"
fi
# 清理前一次编译
if [ -f "Makefile" ]; then
make clean
fi
# 配置OpenSSL - 使用新的平台标识符
./Configure ${OPENSSL_TARGET} no-shared no-dso no-hw no-engine -fPIC --prefix="${OUTPUT_DIR}"
# 编译和安装
make -j$(sysctl -n hw.ncpu)
make install_sw
}
# 构建设备架构 (arm64)
build_for_arch "arm64" "iphoneos" "ios64-xcrun"
# 构建模拟器架构 (arm64-simulator)
build_for_arch "arm64-simulator" "iphonesimulator" "iossimulator-arm64-xcrun"
echo "== 构建完成 =="
echo "库文件位于:"
echo "- ${BUILD_DIR}/arm64/lib (arm64设备架构)"
echo "- ${BUILD_DIR}/arm64-simulator/lib (arm64模拟器架构)"
echo "头文件位于各架构目录的include文件夹中"
echo ""
echo "如何在项目中使用:"
echo "1. 添加include目录到头文件搜索路径"
echo "2. 添加lib目录到库搜索路径"
echo "3. 链接libssl.a和libcrypto.a静态库"
echo "4. 根据目标平台选择正确的库文件目录"
chmod u+x xx.sh之后直接运行,开袋即食。